4

Hey so I have been working on a project that I want to be able to run as an executable jar from the command line. I have been able to create the jar with dependencies using Mavens assembly:single command. My pom looks like this.

<build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>org.openmetadata.main.OmadUpdate</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>

The build is successful and creates the jar omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar. I go to my projects target folder in the command line and type

java -jar omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar

I have also tried

java -cp omad-update-0.0.1-SNAPSHOT-jar-with-dependencies.jar org.openmetadata.main.OmadUpdate

Unfortunately in each case I am given a java.lang.NoClassDefFoundError: org/openmetadata/main/OmadUpdate. I am confused because I know my main class is in the package org.openmetadata.main and yet it is not found. I find this especially confusing because in my pom I specify that class as my main class. I have tried changing the name of the main class to src.main.java.org.openmetadata.main.OmadUpdate and simply OmadUpdate as well but neither seems to have an effect. Thanks for any help in advance.

decal
  • 987
  • 2
  • 14
  • 39

3 Answers3

2

I do not see a Class-Path entry in the manifest above, but your very long filename mentions dependencies. If there are jars within this jar file that your program is dependent on, you must enumerate them in the Class-Path section. See Adding Classes to the JAR File's Classpath for more details.

Mike
  • 1,924
  • 14
  • 16
  • but according to this page [link](http://maven.apache.org/plugins/maven-assembly-plugin/usage.html#aResources) it doesnt appear that I have to specify a classpath, it seems like it will simply package the maven dependencies as needed. I assume I could be making a mistake assuming that though. – decal Mar 28 '12 at 20:23
  • Open up your jar file and look for the MANIFEST.MF file in the META-INF directory. Is a classpath specified? – Mike Mar 28 '12 at 20:25
  • 1
    Alright so I found on Mavens [website](http://maven.apache.org/shared/maven-archiver/examples/classpath.html#aMake) that adding true to my pom.xml should take care of the classpath, However I am still having the problem that my Main class cant be found when I try to run the executable jar. – decal Mar 30 '12 at 14:43
  • Post the contents of MANIFEST.MF. Also verify that the jar file contains the following structure 'org/openmetadata/main/OmadUpdate.class' – Mike Mar 30 '12 at 15:01
  • Manifest-Version: 1.0 Built-By: Andrew Build-Jdk: 1.6.0_27 Created-By: Apache Maven Main-Class: org.openmetadata.main.OmadUpdate Archiver-Version: Plexus Archiver Here is my manifest, You are correct that there is no classpath, but according to the website I referenced in my last response one should be added by Maven as far as I understand... – decal Mar 30 '12 at 15:20
  • also the structure has all my dependencies that are needed in it, including org/openmetadata/main/OmadUpdate.class for some weird reason though, when I try to run the program it says java.lang.ClassNotFoundException: org.openmetadata.main.OmadUpdate – decal Mar 30 '12 at 16:06
  • So, add the proper classpath definition to the manifest and you should be all set. – Mike Mar 30 '12 at 18:30
0

Another option might be to use the onejar-maven-plugin. Unfortunately the usage page is a bit scarce, but the plugin does what is supposed to when configured correctly.

matsev
  • 32,104
  • 16
  • 121
  • 156
0

I finally have been able to get this to work by adding the following code to my pom.

    <build>
    <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>2.3</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>org.openmetadata.omadupdate.OmadUpdate</mainClass>
                    </manifest>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id> <!-- this is used for inheritance merges -->
                <phase>package</phase> <!-- bind to the packaging phase -->
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Without the executions tag in the pom along with its children only the maven dependencies will be added to the jar and the classes from the project itself will not be added.

decal
  • 987
  • 2
  • 14
  • 39