简体   繁体   English

运行可执行jar时ClassNotFoundException

[英]ClassNotFoundException when running executable jar

I created a jar file for utility files and added it as a dependency for my main project. 我为实用程序文件创建了一个jar文件,并将其添加为我的主项目的依赖项。 When I run mvn install to build my main project, the correct utility jar is used in my unit tests. 当我运行mvn install来构建我的主项目时,在我的单元测试中使用了正确的实用程序jar。

However, when I later run the main project jar using java -jar supervisor.jar , I get a java.lang.NoClassDefFoundError: caused by java.lang.ClassNotFoundException: com.seeq.utilities.process.OperatingSystem . 但是,当我稍后使用java -jar supervisor.jar运行主项目jar时,我得到一个java.lang.NoClassDefFoundError:java.lang.ClassNotFoundException: com.seeq.utilities.process.OperatingSystem引起java.lang.ClassNotFoundException: com.seeq.utilities.process.OperatingSystem

In my unit tests, OperatingSystem is used and the tests run fine, so I am assuming that the utility jar is not found when by java when using java -jar . 在我的单元测试中,使用了OperatingSystem并且测试运行正常,所以我假设当使用java -jar时java没有找到实用程序jar。 Is that the issue and how could I fix it? 这是问题,我该如何解决?

Exception in thread "main" java.lang.NoClassDefFoundError: com/seeq/utilities/process/OperatingSystem
    at com.seeq.supervisor.util.ProcessManager.buildCommand(ProcessManager.java:78)
    at com.seeq.supervisor.util.ProcessManager.<init>(ProcessManager.java:27)
    at com.seeq.supervisor.Supervisor.start(Supervisor.java:40)
    at com.seeq.supervisor.Main.main(Main.java:21)
Caused by: java.lang.ClassNotFoundException: com.seeq.utilities.process.OperatingSystem
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 4 more

How I added the dependency in my pom.xml: 我如何在我的pom.xml中添加依赖项:

<dependency>
    <groupId>com.seeq.utilities</groupId>
    <artifactId>seeq-utilities</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/../common/libraries/java/seeq-utilities/target/seeq-utilities-1.0.jar</ systemPath>
</dependency>

If the not found class is inside a jar, you have to provide the classpath arguments; 如果找不到的类在jar中,则必须提供classpath参数; for example: 例如:

java -cp a.jar -jar supervisor.jar

If you want to package your jar and its dependencies together into a single executable uber jar, consider using the maven shade plugin http://maven.apache.org/plugins/maven-shade-plugin/ . 如果您想将jar及其依赖项打包到一个可执行的uber jar中,请考虑使用maven shade插件http://maven.apache.org/plugins/maven-shade-plugin/ An executable uber jar is convenient because you can run it immediately without extra "java -cp" steps. 可执行的超级jar很方便,因为你可以立即运行它而无需额外的“java -cp”步骤。

An example pom plugin entry for an executable uber jar is show below (also see http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html ). 下面显示了可执行uber jar的示例pom插件条目(另请参阅http://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html )。

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-shade-plugin</artifactId>
   <version>2.3</version>
   <executions>
      <execution>
         <phase>package</phase>
         <goals>
            <goal>shade</goal>
         </goals>
         <configuration>
            <transformers>
               <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                  <mainClass>org.example.MyMainClass/mainClass>
               </transformer>
            </transformers>
         </configuration>
      </execution>
   </executions>
</plugin>

This article also gives a nice overview of what the maven shade plugin can do: http://steveperkins.net/batteries-included-distribution-using-maven-shaded-jars/ . 本文还概述了maven shade插件可以做什么: http//steveperkins.net/batteries-included-distribution-using-maven-shaded-jars/

One annoying bug with the maven shade plugin is that it will give duplicate class warnings if you run mvn package more than once. maven shade插件的一个令人讨厌的错误是,如果你多次运行mvn package ,它会给出重复的类警告。 This issue is still unaddressed as of maven-shader-plugin 2.0: http://jira.codehaus.org/browse/MSHADE-126 ). 从maven-shader-plugin 2.0开始,这个问题仍然没有得到解决: http//jira.codehaus.org/browse/MSHADE-126 )。 To work around the issue, you can re-create your jar from scratch on each invocation, with the following maven-jar-plugin configuration: 要解决此问题,您可以在每次调用时从头开始重新创建jar,使用以下maven-jar-plugin配置:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-jar-plugin</artifactId>
   <version>2.4</version>
   <configuration>
      <forceCreation>true</forceCreation>
   </configuration>
</plugin>

Another issue that you may run into is that re-packaging signed jars invalidates their signatures. 您可能遇到的另一个问题是重新打包签名的jar会使其签名无效。 If that causes errors for you, see this answer: https://stackoverflow.com/a/6743609/1454388 如果这会导致您的错误,请参阅以下答案: https//stackoverflow.com/a/6743609/1454388

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM