[英]Why is the Java -cp option not working when -jar does while using the spring-boot-maven-plugin?
I have a simple maven project that creates an executable jar file named test.jar
in the target
directory.我有一个简单的 maven 项目,它在
target
目录中创建一个名为test.jar
的可执行 jar 文件。
<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... 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
我主要得到...
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.我还尝试了其他版本,例如
java -cp "target\test.jar" com.me.Main
但无论如何它似乎都不起作用。 What am I missing?我错过了什么?
Update更新
The MANIFEST.mf
looks like this MANIFEST.mf
看起来像这样
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.这似乎表明 spring-boot 正在覆盖 Main-Class KV。
If I change to the maven-assembly-plugin
如果我更改为
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
那么我仍然可以使用
java -jar
运行,也可以使用java -cp
运行
check the contents of JAR file:检查 JAR 文件的内容:
jar tvf test.jar
should contain a com/me/Main.class
应该包含一个
com/me/Main.class
and contents of its MANIFEST.MF file:及其 MANIFEST.MF 文件的内容:
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
)并使用文本编辑器打开
META-INF/MANIFEST.MF
- 应包含以Main-Class:
以及主 class 的完全限定名称,这是一个带有main
方法的名称,在启动 JAR 时使用(使用java -jar
)
(or just use WinZIP, 7-zip, or similar to open and check the JAR file) (或仅使用 WinZIP、7-zip 或类似软件打开并检查 JAR 文件)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.