问题
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
I run it by command java -jar ChatApp1-1.0-SNAPSHOT.jar
And here is my dependency info:
<dependencies>
<dependency>
<groupId>org.jgroups</groupId>
<artifactId>jgroups</artifactId>
<version>3.4.3.Final</version>
</dependency>
</dependencies>
What could be wrong?
回答1:
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.
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>
回答2:
The whole point of an Integrated Development Environment is to setup and manage your source code and its dependencies.
Eclipse does this one way, Netbeans does it another way, and so does IntelliJ.
You can try to reproduce it by simulating Eclipse's Deployment Assembly. Find out where your libraries and source code is stored. Use those directories for your javac command. Then execute java with the class you want that contains a main method.
This gets harder if your project is meant to be a web application.
You really should let the IDE do it for you.
来源:https://stackoverflow.com/questions/22990475/java-lang-noclassdeffounderror-when-running-maven-jgroups-project-build-with-net