I have a simple maven project that creates an executable jar file named test.jar
in the target
directory.
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<main.class>com.me.Main</main.class>
</properties>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<mainClass>${main.class}</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
When I run with java -jar target\test.jar
everything runs fine but when I try to use the -cp
to run the jar without the -jar
so I can add things to the classpath like (Windows) java -cp target\test.jar com.me.Main
I get...
Error: Could not find or load main class com.me.Main
Caused by: java.lang.ClassNotFoundException: com.me.Main
I also tried other versions like java -cp "target\test.jar" com.me.Main
but no matter what it doesn't seem to work. What am I missing?
Update
The MANIFEST.mf
looks like this
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.me.Main
Spring-Boot-Version: 2.2.12.RELEASE
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
This seems to show that spring-boot is overriding the Main-Class KV.
If I change to the maven-assembly-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<finalName>${project.name}-${project.version}</finalName>
<archive>
<manifest>
<mainClass>
${main.class}
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
then I can still run as java -jar
and I can also run with java -cp
check the contents of JAR file:
jar tvf test.jar
should contain a com/me/Main.class
and contents of its MANIFEST.MF file:
jar xf test.jar META-INF/MANIFEST.MF
and use a text editor to open META-INF/MANIFEST.MF
- should contain line starting with Main-Class:
and the fully qualified name of the main class, the one with the main
method, used when starting the JAR (with java -jar
)
(or just use WinZIP, 7-zip, or similar to open and check the JAR file)
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.