NoClassDefFoundError while executing main class using java -classpath command

后端 未结 1 797
逝去的感伤
逝去的感伤 2020-12-11 11:12

I have a Maven project and within that I have a Main.java class that contains the main() method that I would like to execute from Windows command p

相关标签:
1条回答
  • 2020-12-11 11:24

    Was able to get the program working by adding following plugins in the pom.

    The first dependency creates the META-INF/manifest.mf file in the jar and the second one creates the lib folder in the existing target directory and copies all the dependencies in there.

      <plugin>
            <!-- Build an executable JAR -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>com.ebayenterprise.ecp.jobs.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>                     
        <plugin>
            <!-- Build an executable JAR with runtime dependencies so that this program can be executed from command line using java -jar command -->
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.10</version>
            <executions>
                <execution>
                    <id>copy-dependencies</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy-dependencies</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>${project.build.directory}/lib</outputDirectory>
                        <overWriteReleases>false</overWriteReleases>
                        <overWriteSnapshots>false</overWriteSnapshots>
                        <overWriteIfNewer>true</overWriteIfNewer>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    
    0 讨论(0)
提交回复
热议问题