. lang。在使用netbeans运行maven jgroups项目构建时使用NoClassDefFoundError

时间:2022-08-10 17:27:22

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:

我正在做一个聊天项目。当我在ide (netbeans)中运行它时,它正常地打开,并且工作得很好。但是当我从终端运行时,我得到的误差是这样的:

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

线程“main”java.lang中的异常。NoClassDefFoundError: org/jgroups/Receiver at com.mycompany.chatapp1.ChatWindow. (ChatWindow.java:32) at com.mycompany.chatapp1.Main.main(Main.java:10)由:java.lang引起。ClassNotFoundException:org.jgroups。urlclassloader $1.run(URLClassLoader.java:372)位于java.net.URLClassLoader .run(URLClassLoader.java:361)位于java.security.AccessController。在java.net.URLClassLoader.findClass(URLClassLoader.java:360) at java.lang.ClassLoader.loadClass(ClassLoader.java:424)上,在java.lang.ClassLoader.loadClass(ClassLoader.java: java:357)上,加载类(Launcher.java:308)。2

I run it by command java -jar ChatApp1-1.0-SNAPSHOT.jar

我通过命令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>

这是我的依赖信息: <依赖> <依赖> org。jgroups < / groupId > < artifactId > jgroups < / artifactId > <版本> 3.4.3。最后< /版本> < /依赖> < / >的依赖关系

What could be wrong?

可能是错的呢?

2 个解决方案

#1


3  

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.

创建jar项目时,不包含依赖项目。因此,您需要通过-cp在命令行上设置类路径,这将非常麻烦,或者您可以使用Maven Shade插件,它包含您jar中的所有依赖项,从而生成一个完整的、可执行的jar文件。

Include the following snippet in your pom (of course with your main class):

在你的pom(当然包括你的主类)中包含以下代码片段:

  <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


0  

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.

Eclipse这样做,Netbeans这样做,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.

您可以尝试通过模拟Eclipse的部署程序集来复制它。找出你的库和源代码存储在哪里。将这些目录用于javac命令。然后使用包含主方法的类执行java。

This gets harder if your project is meant to be a web application.

如果您的项目是web应用程序,那么这将变得更加困难。

You really should let the IDE do it for you.

你真的应该让IDE来帮你。

#1


3  

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.

创建jar项目时,不包含依赖项目。因此,您需要通过-cp在命令行上设置类路径,这将非常麻烦,或者您可以使用Maven Shade插件,它包含您jar中的所有依赖项,从而生成一个完整的、可执行的jar文件。

Include the following snippet in your pom (of course with your main class):

在你的pom(当然包括你的主类)中包含以下代码片段:

  <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


0  

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.

Eclipse这样做,Netbeans这样做,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.

您可以尝试通过模拟Eclipse的部署程序集来复制它。找出你的库和源代码存储在哪里。将这些目录用于javac命令。然后使用包含主方法的类执行java。

This gets harder if your project is meant to be a web application.

如果您的项目是web应用程序,那么这将变得更加困难。

You really should let the IDE do it for you.

你真的应该让IDE来帮你。