问题描述
我正在做一个聊天项目.当我在ide(netbeans)中运行它时,它可以正常打开并运行良好.但是当我从终端运行它时,我得到了这样的错误:
I am making a chat project. When i run it inside ide (netbeans) it opens normally and works great. But when i run it from terminal i'm getting error like this:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jgroups/Receiver
at com.mycompany.chatapp1.ChatWindow.<init>(ChatWindow.java:32)
at com.mycompany.chatapp1.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: org.jgroups.Receiver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
Exception in thread "main" java.lang.NoClassDefFoundError: org/jgroups/Receiver
at com.mycompany.chatapp1.ChatWindow.<init>(ChatWindow.java:32)
at com.mycompany.chatapp1.Main.main(Main.java:10)
Caused by: java.lang.ClassNotFoundException: org.jgroups.Receiver
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
我通过命令java -jar ChatApp1-1.0-SNAPSHOT.jar
这是我的依赖项信息:<dependencies> <dependency> <groupId>org.jgroups</groupId> <artifactId>jgroups</artifactId> <version>3.4.3.Final</version> </dependency> </dependencies>
And here is my dependency info:<dependencies> <dependency> <groupId>org.jgroups</groupId> <artifactId>jgroups</artifactId> <version>3.4.3.Final</version> </dependency> </dependencies>
可能是什么问题?
推荐答案
创建jar项目时,不包括相关项目.因此,您可能需要通过-cp
在命令行上设置类路径,这会非常麻烦,或者您可以使用 Maven Shade插件,其中包含您jar中的所有依赖项,从而生成了一个完整的可执行jar文件.
When you create a jar project, dependent projects are not included. So you would either need to set the classpath on the commandline via -cp
which would be quite cumbersome, or you could use the Maven Shade Plugin, which includes all your dependencies in you jar, resulting in a complete, executable jar file.
在pom中包含以下代码段(当然,对于您的主要课程):
Include the following snippet in your pom (of course with your main class):
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>my.main.class</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这篇关于使用NetBeans运行Maven jgroups项目构建时出现java.lang.NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!