我在 netbeans 中有 Maven 项目。我的 POM 看起来像这样:
<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>myproject</groupId>
<artifactId>Myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Slave</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>myproject.slave.App</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-my-jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
</dependency>
</dependencies>
</project>
它工作得很好,我可以执行Myproject-1.0-SNAPSHOT-jar-with-dependencies.jar
并且 Java 找到了主类并且它正确执行 - 直到某个时刻。突然,当我想执行 jar 文件时,我得到了异常:
java -jar Myproject-1.0-SNAPSHOT-jar-with-dependencies.jar
Exception in thread "main" java.lang.NullPointerException
at sun.launcher.LauncherHelper.getMainClassFromJar(Unknown Source
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
java -cp Myproject-1.0-SNAPSHOT-jar-with-dependencies.jar myproject.slave.App
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/nodes/Eleme
nt
at myproject.slave.Worker.<init>(Worker.java:29)
at myproject.slave.App.main(App.java:26)
Caused by: java.lang.ClassNotFoundException: org.jsoup.nodes.Element
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
当我在 Netbeans 中单击“运行”时,它会正确执行。
所以问题是具有依赖关系的 jar 构建不正确,我只是想知道为什么?我就像2小时前一样,现在它不工作了......