简体   繁体   English

尽管有清单,但在jar文件中找不到主类

[英]main class not found in jar file despite manifest

I have Maven project in netbeans. 我在netbeans中有Maven项目。 My POM looks like this: 我的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>

It worked fine, I could execute Myproject-1.0-SNAPSHOT-jar-with-dependencies.jar and Java found main class and it executed correctly - Until some moment. 它工作正常,我可以执行Myproject-1.0-SNAPSHOT-jar-with-dependencies.jar ,Java找到了主类,并且它正确执行-直到片刻。 Suddenly when I wanted to execute jar file I got exception: 突然,当我想执行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)

When I click "Run" in Netbeans it executes correctly. 当我在Netbeans中单击“运行”时,它将正确执行。

So the problem is that jar with dependencies is build incorrectly, I just wonder why? 所以问题是带有依赖项的jar的构建不正确,我只是想知道为什么吗? I was like 2 hours ago and now it's not working... 我当时像2个小时前,现在却无法正常工作...

OK, so the problem is NOT that the JVM can't find the main class. OK,所以问题不在于JVM找不到主类。 The class that it cannot find is "org.jsoup.nodes.Element". 它找不到的类是“ org.jsoup.nodes.Element”。 (The stack trace says so ...) (堆栈跟踪如此表示...)

But why? 但为什么? After all you have included jsoup as a dependency in the POM file ... 毕竟,您已将jsoup作为依赖项包含在POM文件中...

The reason is that that the JVM's class loader cannot find classes in nested JARs. 原因是JVM的类加载器无法在嵌套JAR中找到类。 If you want to create an executable JAR that is self-contained (ie that contains all of the dependent classes it needs), you need to use the maven-shade-plugin to create a so-called "uber-jar". 如果要创建独立的可执行JAR(即包含所需的所有依赖类),则需要使用maven-shade-plugin创建所谓的“ uber-jar”。


When you are running from Netbeans, the command launcher is setting up the classpath ... and not using the -jar option to launch the JVM. 从Netbeans运行时,命令启动器正在设置类路径...,而不使用-jar选项启动JVM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM