第一次使用maven后的Java

第一次使用maven后的Java

本文介绍了第一次使用maven后的Java.lang.NoClassDefFoundError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在为我的公司创建一个带有java的soap服务器,最近我转而使用Bone-CP和Maven导入所有必需的第三方程序。在我完成实现bone-CP后,我使用了服务器命令

I've been creating a soap server with java for my company and I just recently switched over to using Bone-CP and Maven to import all the required 3rd party programs. After I finished implementing bone-CP I used the server command

jar -cvfm SoapServer.jar manifest.txt SoapServer

当我将它转移到我的服务器并试图运行它时出现了这个错误:

And when I transfered it to my server and tried to run it I got this error:

Exception in thread "main" java.lang.NoClassDefFoundError: SoapServer/SoapServer (wrong name: com/test/SoapServer/SoapServer)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    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:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

我认为导致问题的唯一想法是maven没有将所需的JAR打包到程序中?如果是这样的话,我只需要下载它们并将它们添加到类路径中吗?

The only think I can think of causing the problem is that maven doesn't package the required JARs with the program? If that's the case do I just need to download them and add them to the class path?

推荐答案

如果您需要所有的要包装在可执行jar中的依赖项,然后配置你的pom,如

If you need all of your dependencies to be packed in the executable jar then configure your pom like

<build>
    <plugins>
      <plugin>
         <artifactId>maven-jar-plugin</artifactId>
         <configuration>
           <archive>
             <manifest>
               <addClasspath>true</addClasspath>
               <classpathPrefix>lib/</classpathPrefix>
               <mainClass>com.something.YourMainClass</mainClass>
             </manifest>
           </archive>
         </configuration>
      </plugin>
    </plugins>
  </build>

上面的块会将lib /中的所有库添加到清单类路径条目中的类路径

above block will add all the library in lib/ to classpath in manifest classpath entry

并将所有依赖项复制到lib目录

and to copy all the dependencies to lib directory

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <version>2.1</version>
        <executions>
          <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
              <goal>copy-dependencies</goal>
            </goals>
            <configuration>
              <outputDirectory>${project.build.directory}/lib</outputDirectory>
              <overWriteReleases>false</overWriteReleases>
              <overWriteSnapshots>false</overWriteSnapshots>
              <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
          </execution>
        </executions>
      </plugin>

这篇关于第一次使用maven后的Java.lang.NoClassDefFoundError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 15:29