![](/img/trans.png)
[英]spring-boot-maven-plugin created jar run by using classpath
[英]Why is the Java -cp option not working when -jar does while using the spring-boot-maven-plugin?
我有一个简单的 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
我主要得到...
Error: Could not find or load main class com.me.Main
Caused by: java.lang.ClassNotFoundException: com.me.Main
我还尝试了其他版本,例如java -cp "target\test.jar" com.me.Main
但无论如何它似乎都不起作用。 我错过了什么?
更新
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/
这似乎表明 spring-boot 正在覆盖 Main-Class KV。
如果我更改为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>
那么我仍然可以使用java -jar
运行,也可以使用java -cp
运行
检查 JAR 文件的内容:
jar tvf test.jar
应该包含一个com/me/Main.class
及其 MANIFEST.MF 文件的内容:
jar xf test.jar META-INF/MANIFEST.MF
并使用文本编辑器打开META-INF/MANIFEST.MF
- 应包含以Main-Class:
以及主 class 的完全限定名称,这是一个带有main
方法的名称,在启动 JAR 时使用(使用java -jar
)
(或仅使用 WinZIP、7-zip 或类似软件打开并检查 JAR 文件)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.