"Error: Could not find or load main class com.mycompany.App" when trying to execute Maven-generated Jar

StackOverflow https://stackoverflow.com/questions/16972320

Question

I'm trying to build a maven java program on on Windows7 and my java -version is 1.7. I'm getting this error:

"Error: Could not find or load main class com.mycompany.App" when trying to execute Maven-generated Jar

even though com/mycompany/App.class exists within the Jar and it has a main method. Here is what my META-INF/MANIFEST.MF looks like:

Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: me
Build-Jdk: 1.7.0_03
Main-Class: com.mycompany.App
Class-Path: C:/Users/me/.m2/repository/com/mycompany/commons/2.1-SNAPSHOT/commons-2.1-SNAPSHOT.jar

I'm trying to execute the jar using:

java -jar mycompany-2.1-SNAPSHOT.jar 12:141678 1

There is a blank line at the end of my Manifest file. I'm using Maven 3.0.5 and using mvn package to generate the jar file. Here is a portion of my pom.xml:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        <configuration>
            <archive>
                <manifest>
                    <addClasspath>true</addClasspath>
                    <classpathLayoutType>repository</classpathLayoutType>
                    <classpathPrefix>${settings.localRepository}</classpathPrefix>
                    <mainClass>com.mycompany.App</mainClass>
                </manifest>
            </archive>
        </configuration>
    </plugin>

Can someone help me understand what is causing this error?

Was it helpful?

Solution

If you remove everything from manifest except the mainClass, it should work. If you are trying to create an executable jar file that includes all of the classpath dependencies, you probably want to use either the assembly plugin or the shade plugin instead of the jar plugin.

OTHER TIPS

  • assuming you have already checked that those files exist:

./mycompany-2.1-SNAPSHOT.jar

C:/Users/me/.m2/repository/com/mycompany/commons/2.1-SNAPSHOT/commons-2.1-SNAPSHOT.jar

  • Does the artifact contain com/mycompany/App.class ?

jar -tf mycompany-2.1-SNAPSHOT.jar

  • Running java with the option -verbose:class can show how the classpath is resolved to load (or not) each class.

Note

You might have a warning at build time because no version is specified for the plugin:

    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>2.4</version>
    ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top