我正在使用 Netbeans IDE 21。该应用程序在从 IDE 运行时运行良好。我已准备好部署并尝试从 jar 文件运行它,但每次都会抛出相同的异常。操作系统是Linux Mint 21.3。 Java 的版本是 JDK 19。是的,我已经修改了 pom.xml 以修复其他错误,我怀疑这就是问题所在,但我束手无策。我现在确切地知道抛出异常的位置,是在实例化串行端口对象时。我使用存档管理器检查了 .jar 文件,看起来该库没有与构建一起打包。任何建议将不胜感激。
david@david-OptiPlex-9010:~/Documents/Netbeans/ParticleCountAlarm/target$ java -jar ParticleCountAlarm-1.0.jar |more
Exception in thread "main" java.lang.NoClassDefFoundError: com/fazecast/jSerialComm/SerialPort
at alk.okc.particlecountalarm.mySerialPort.connect(mySerialPort.java:20)
at alk.okc.particlecountalarm.mySerialPort.<init>(mySerialPort.java:15)
at alk.okc.particlecountalarm.MainFrame.startSerialTimer(MainFrame.java:82)
at alk.okc.particlecountalarm.MainFrame.<init>(MainFrame.java:17)
at alk.okc.particlecountalarm.ParticleCountAlarm.main(ParticleCountAlarm.java:6)
Caused by: java.lang.ClassNotFoundException: com.fazecast.jSerialComm.SerialPort
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
... 5 more
<?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>ALK.OKC</groupId>
<artifactId>ParticleCountAlarm</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.fazecast</groupId>
<artifactId>jSerialComm</artifactId>
<version>2.10.4</version>
<type>jar</type>
</dependency>
</dependencies>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<exec.mainClass>alk.okc.particlecountalarm.ParticleCountAlarm</exec.mainClass>
</properties>
<build>
<plugins>
<!-- Maven JAR Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>alk.okc.particlecountalarm.ParticleCountAlarm</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<!-- Maven Source Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.3.1</version> <!-- Use the latest version here -->
</plugin>
<!-- Maven Javadoc Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.6.3</version> <!-- Use the latest version here -->
</plugin>
<!-- Maven Deploy Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>3.1.1</version> <!-- Use the latest version here -->
</plugin>
</plugins>
</build>
</project>
我从 Maven 切换到 Ant,并在 5 分钟内解决了这个问题,此前我花了几个小时尝试使用 Maven 打包单个库,但没有成功。不是 Maven 的忠实粉丝。