运行Maven项目显示错误" NoClassDefFoundError:purejavacomm / CommPortIdentifier"

时间:2015-03-09 12:47:31

标签: java maven netbeans command-line serial-port

我使用“purejavacomm-0.0.22.jar”编写了一个串口代码,如果通过NetBeans id 8.0.2运行Build Success输出

[ttyUSB0, ttyS3, ttyS2, ttyS1, ttyS0]

但我使用via terminal

mvn package

[信息]建立成功 但是运行项目

java -jar target/PureJavaComm-1.0-SNAPSHOT.jar 

我会收到错误

Exception in thread "main" java.lang.NoClassDefFoundError: purejavacomm/CommPortIdentifier
    at com.mycompany.purejavacommu.PureJavaComm.main(PureJavaComm.java:28)
Caused by: java.lang.ClassNotFoundException: purejavacomm.CommPortIdentifier
    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)
    ... 1 more

我的pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mycompany</groupId>
    <artifactId>PureJavaComm</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <mainClass>com.mycompany.purejavacommu.PureJavaComm</mainClass>
    </properties>
    <dependencies>       
        <dependency>
            <groupId>com.sparetimelabs</groupId>
            <artifactId>purejavacomm</artifactId>
            <version>0.0.22</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>com.mycompany.purejavacommu.PureJavaComm</mainClass>                            
                        </manifest>
                    </archive>
                </configuration>          
            </plugin>
        </plugins>  
    </build>
</project>

3 个答案:

答案 0 :(得分:0)

也许问题是在netbeans上设置m2_repo路径变量,所以它在编码时运行正常但是一旦你编译了jar它就不知道在哪里搜索PureJavaComm库

答案 1 :(得分:0)

ClassNotFoundException指示在尝试执行程序时未在类路径中找到该类。您应该在运行时将purejavacomm库添加到类路径中。一种方法是通过命令行-cp修饰符:

-cp target/lib/<dependency> -jar ...

因此,在您的情况下,它可能最终看起来像:

java -cp target/lib/purejavacomm.jar -jar target/PureJavaComm-1.0-SNAPSHOT.jar

答案 2 :(得分:0)

我使用过Apache Maven Shade插件: 这个插件提供了将工件打包在超级jar中的功能,包括它的依赖关系和阴影。

<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>

参考此解决方案java.lang.NoClassDefFoundError when running maven jgroups project build with netbeans