2

我在 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小时前一样,现在它不工作了......

4

1 回答 1

1

好的,所以问题不在于 JVM 找不到主类。它找不到的类是“org.jsoup.nodes.Element”。(堆栈跟踪是这样说的......)

但为什么?毕竟你已经在 POM 文件中包含了 jsoup 作为依赖项......

原因是 JVM 的类加载器无法在嵌套的 JAR 中找到类。如果你想创建一个自包含的可执行 JAR(即包含它需要的所有依赖类),你需要使用maven-shade-plugin创建一个所谓的“uber-jar”。


当您从 Netbeans 运行时,命令启动器正在设置类路径......而不是使用-jar启动 JVM 的选项。

于 2013-01-31T12:44:57.453 回答