我在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小时前一样现在它不起作用......
答案 0 :(得分:1)
好的,问题不在于JVM找不到主类。它找不到的类是“org.jsoup.nodes.Element”。 (堆栈跟踪说明了......)
但为什么呢?毕竟你已经将jsoup作为POM文件中的依赖项...
原因是JVM的类加载器无法在嵌套的JAR中找到类。如果要创建一个自包含的可执行JAR(即包含所需的所有依赖类),则需要使用maven-shade-plugin创建一个所谓的“超级jar”。
当您从Netbeans运行时,命令启动程序正在设置类路径...而不是使用-jar
选项来启动JVM。