score:249

Accepted answer

actually, i think that the answer given in the question you mentioned is just wrong (update - 20101106: someone fixed it, this answer refers to the version preceding the edit) and this explains, at least partially, why you run into troubles.


it generates two jar files in logmanager/target: logmanager-0.1.0.jar, and logmanager-0.1.0-jar-with-dependencies.jar.

the first one is the jar of the logmanager module generated during the package phase by jar:jar (because the module has a packaging of type jar). the second one is the assembly generated by assembly:assembly and should contain the classes from the current module and its dependencies (if you used the descriptor jar-with-dependencies).

i get an error when i double-click on the first jar:

could not find the main class: com.gorkwobble.logmanager.logmanager. program will exit.

if you applied the suggested configuration of the link posted as reference, you configured the jar plugin to produce an executable artifact, something like this:

  <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-jar-plugin</artifactid>
    <configuration>
      <archive>
        <manifest>
          <addclasspath>true</addclasspath>
          <mainclass>com.gorkwobble.logmanager.logmanager</mainclass>
        </manifest>
      </archive>
    </configuration>
  </plugin>

so logmanager-0.1.0.jar is indeed executable but 1. this is not what you want (because it doesn't have all dependencies) and 2. it doesn't contain com.gorkwobble.logmanager.logmanager (this is what the error is saying, check the content of the jar).

a slightly different error when i double-click the jar-with-dependencies.jar:

failed to load main-class manifest attribute from: c:\eclipseprojects\logmanager\target\logmanager-0.1.0-jar-with-dependencies.jar

again, if you configured the assembly plugin as suggested, you have something like this:

  <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-assembly-plugin</artifactid>
    <configuration>
      <descriptorrefs>
        <descriptorref>jar-with-dependencies</descriptorref>
      </descriptorrefs>
    </configuration>
  </plugin>

with this setup, logmanager-0.1.0-jar-with-dependencies.jar contains the classes from the current module and its dependencies but, according to the error, its meta-inf/manifest.mf doesn't contain a main-class entry (its likely not the same manifest.mf as in logmanager-0.1.0.jar). the jar is actually not executable, which again is not what you want.


so, my suggestion would be to remove the configuration element from the maven-jar-plugin and to configure the maven-assembly-plugin like this:

  <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-jar-plugin</artifactid>
    <version>2.2</version>
    <!-- nothing here -->
  </plugin>
  <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-assembly-plugin</artifactid>
    <version>2.2-beta-4</version>
    <configuration>
      <descriptorrefs>
        <descriptorref>jar-with-dependencies</descriptorref>
      </descriptorrefs>
      <archive>
        <manifest>
          <mainclass>org.sample.app</mainclass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>single</goal>
        </goals>
      </execution>
    </executions>
  </plugin>

of course, replace org.sample.app with the class you want to have executed. little bonus, i've bound assembly:single to the package phase so you don't have to run assembly:assembly anymore. just run mvn install and the assembly will be produced during the standard build.

so, please update your pom.xml with the configuration given above and run mvn clean install. then, cd into the target directory and try again:

java -jar logmanager-0.1.0-jar-with-dependencies.jar

if you get an error, please update your question with it and post the content of the meta-inf/manifest.mf file and the relevant part of your pom.xml (the plugins configuration parts). also please post the result of:

java -cp logmanager-0.1.0-jar-with-dependencies.jar com.gorkwobble.logmanager.logmanager

to demonstrate it's working fine on the command line (regardless of what eclipse is saying).

edit: for java 6, you need to configure the maven-compiler-plugin. add this to your pom.xml:

  <plugin>
    <groupid>org.apache.maven.plugins</groupid>
    <artifactid>maven-compiler-plugin</artifactid>
    <configuration>
      <source>1.6</source>
      <target>1.6</target>
    </configuration>
  </plugin>

score:-1

right click the project and give maven build,maven clean,maven generate resource and maven install.the jar file will automatically generate.

score:5

if you don't want execute assembly goal on package, you can use next command:

mvn package assembly:single

here package is keyword.

score:17

the answer of pascal thivent helped me out, too. but if you manage your plugins within the <pluginmanagement>element, you have to define the assembly again outside of the plugin management, or else the dependencies are not packed in the jar if you run mvn install.

<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
    xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>
    <version>1.0.0-snapshot</version>
    <packaging>jar</packaging>


    <build>
        <pluginmanagement>
            <plugins>

                <plugin>
                    <groupid>org.apache.maven.plugins</groupid>
                    <artifactid>maven-compiler-plugin</artifactid>
                    <version>3.1</version>
                    <configuration>
                        <source>1.6</source>
                        <target>1.6</target>
                    </configuration>
                </plugin>

                <plugin>
                    <groupid>org.apache.maven.plugins</groupid>
                    <artifactid>maven-assembly-plugin</artifactid>
                    <version>2.4</version>
                    <configuration>
                        <archive>
                            <manifest>
                                <mainclass>main.app</mainclass>
                            </manifest>
                        </archive>
                        <descriptorrefs>
                            <descriptorref>jar-with-dependencies</descriptorref>
                        </descriptorrefs>
                    </configuration>
                    <executions>
                        <execution>
                            <id>make-assembly</id>
                            <phase>package</phase>
                            <goals>
                                <goal>single</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

            </plugins>

        </pluginmanagement>

        <plugins> <!-- did not work without this  -->
            <plugin>
                <groupid>org.apache.maven.plugins</groupid>
                <artifactid>maven-assembly-plugin</artifactid>
            </plugin>
        </plugins>

    </build>


    <dependencies>
       <!--  dependencies commented out to shorten example -->
    </dependencies>

</project>

Related Query