简体   繁体   中英

Main class not found in JAR

I'm using Maven to build a project I'm working on. The build works fine, the problem happens when I run the generated JAR. The error says the main class com.apress.springrecipes.sequence.Main cannot be found. But looking at the folder structure I'm not seeing why the Main class can't be found.

Here's the folder structure:

myJar.jar
    --com
        --apress
            --springrecipes
                --sequence
                    --Main.class
    --META-INF
        --MAINIFEST.MF

The MANIFEST.MF file has the main class attribute set to com.apress.springrecipes.sequence.Main . But it looks like the JAR can't find it even though the class file is located in the JAR.

Here's the relevant portion of my pom.xml that handles the build:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>C:/Users/Graham/.m2/repository/</classpathPrefix>
                        <classpathLayoutType>repository</classpathLayoutType>
                        <mainClass>com.apress.springrecipes.sequence.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

Why isn't the Main class being found in the jar even though it's there?

I'm not a Java dev, but I give it a try: what the compiler says and what you're checking is not the same thing. The compiler says it cannot find the "Main class", it does not say it cannot find the "Main.class file".

The file may be there, but is there a "class Main..." defined in it (with a public void main() method)?

Even I was facing similar issue, I took below actions and it worked.

  1. Make sure your IDE and the JAVA_HOME environment variable are pointing to the same version of JAVA.
  2. If you have any configuration files (.ini, xml etc) place those along with the JAR file in the same folder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM