gpt4 book ai didi

java - 由于缺少库,从命令行运行可执行 jar 失败

转载 作者:行者123 更新时间:2023-12-02 03:56:02 27 4
gpt4 key购买 nike

我使用以下构建配置从 Maven 创建了一个可执行 jar:

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.targetName}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeScope>test</excludeScope>
<includeScope>compile</includeScope>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<packageName>${project.build.packageName}</packageName>
<mainClass>${project.build.packageName}.${project.build.className}</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<finalName>${project.build.targetName}/${project.build.targetName}</finalName>
</configuration>
</plugin>

生成了以下文件:

./lib/axis-2878297.jar
./lib/axis-wsdl4j-1.5.1.jar
./lib/(other dependencies jars)
./target.jar

1.如果我以这种方式从命令行运行这个 jar:

java -jar target.jar

它产生异常:

Exception in thread "main" java.lang.NoClassDefFoundError: javax/wsdl/OperationType
at org.apache.axis.description.OperationDesc.<clinit>(OperationDesc.java:59)
...

2.如果我以其他方式运行它:

java -cp "target.jar;lib\*" com.test.Main

执行将会成功。

P.S. 我尝试将 -verbose:class 添加到命令中。

对于案例 1,它显示:

[Loaded org.apache.axis.description.OperationDesc from file:/C:/build/lib/axis-2878297.jar]
[Loaded java.lang.Throwable$PrintStreamOrWriter from C:\Program Files\Java\jre1.8.0_74\lib\rt.jar]
[...]

对于案例 2,它显示:

[Loaded org.apache.axis.description.OperationDesc from file:/C:/build/lib/axis-2878297.jar]
[Loaded javax.wsdl.OperationType from file:/C:/build/lib/axis-wsdl4j-1.5.1.jar]
[...]

案例1失败的原因是什么?

谢谢。

最佳答案

好的,我知道原因了:

在 pom.xml 中,我将 axis-wsdl4j 的范围设置为“provided”,这表明我希望 JDK 或容器在运行时提供依赖项。因此,当maven编写manifest.mf时,它会跳过这个依赖关系。

感谢您的所有回答和帮助。

<dependency>
<groupId>axis</groupId>
<artifactId>axis-wsdl4j</artifactId>
<version>1.5.1</version>
<scope>provided</scope>
</dependency>

关于java - 由于缺少库,从命令行运行可执行 jar 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35446057/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com