简体   繁体   English

Intellij Executable Jar - 没有主清单属性 Vs 无法找到或加载主类

[英]Intellij Executable Jar - no main manifest attribute Vs Could not find or load main class

I am developing a small Java application using intellij idea.我正在使用 Intellij 想法开发一个小型 Java 应用程序。 There is a package and a class.有一个包和一个类。 The class runs as expected when outside of the jar format.该类在 jar 格式之外时按预期运行。

I can build a jar and deploy all the dependencies to the same directory and the jar works as expected (I do this with the copy to the output path and link via manifest option when configuring a jar build process).我可以构建一个 jar 并将所有依赖项部署到同一目录,并且 jar 可以按预期工作(在配置 jar 构建过程时,我将复制到输出路径并通过清单选项链接)。 However when I try to build a fat jar with all dependencies included in the same jar file, I get one of two errors depending on where the META-INF/MANIFEST.MF is positioned: no main manifest attribute and Could not find or load main class .但是,当我尝试构建一个包含在同一个 jar 文件中的所有依赖项的胖 jar 时,根据META-INF/MANIFEST.MF的位置,我得到两个错误之一:没有主清单属性无法找到或加载主类

Given that the I could get a working jar file if all the dependencies are in the same directory as said jar file, I assumed that the issue was with the classpath.鉴于如果所有依赖项与所述 jar 文件位于同一目录中,我可以获得一个工作 jar 文件,我认为问题出在类路径上。

The META-INF/MANIFEST.MF contents is as follows: META-INF/MANIFEST.MF内容如下:

Manifest-Version: 1.0
Main-Class: myPackage.myClass
Class-Path: . myClassJarName.jar

When the jar is generated, I see the META-INF/MANIFEST.MF at the root of the structure with the correct information.生成 jar 时,我会在结构的根部看到META-INF/MANIFEST.MF ,其中包含正确的信息。

Also in the root of the jar is the myPackage/myClass.class , corresponding with the manifest file. jar 的根目录中还有myPackage/myClass.class ,对应于清单文件。

I can see all the dependencies in the jar file too.我也可以看到 jar 文件中的所有依赖项。

My projects structure looks like this - note that I have added two manifest lcoations I've tried and the error which comes with each:我的项目结构如下所示 - 请注意,我添加了两个我尝试过的清单文件以及每个文件附带的错误:

 .idea 
 out   
   artifacts
     myPackage_jar
       myPackage.jar
 src
   main
     java
       myPackage
         myClass.java
     META-INF <-------------------- *If here, Error: Could not find or load main class err*
       MANIFEST.MF
   META-INF < ------------------ *If here no main manifest attribute err*
     MANIFEST.MF

   test
     java
       myPackage
         myClass.java
 target
   classes
     myPackage
       myClass
   generated-sources
   generated-test-sources
   test-classes
 mypackage.iml
 pom.xml

... and its pom file looks like this: ...它的 pom 文件如下所示:

<?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>myPackage</groupId>
  <artifactId>myPackage</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>anonymise</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <repositories>
    <repository>
      <id>hortonworks</id>
      <name>hortonworks</name>
      <url>https://repo.hortonworks.com/content/repositories/releases/</url>
    </repository>
  </repositories>

  <dependencies>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
      <groupId>org.apache.hadoop</groupId>
      <artifactId>hadoop-common</artifactId>
      <version>2.7.3.2.6.2.0-205</version>
    </dependency>
    <dependency>
      <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
      <groupId>org.apache.hive</groupId>
      <artifactId>hive-exec</artifactId>
      <version>1.2.1000.2.6.2.69-1</version>
    </dependency>
  </dependencies>

</project>

The way I configured the jar artifact is:我配置jar工件的方式是:

  • Project Structure -> Artifacts项目结构 -> 工件
  • Click + -> Jar -> From modules with dependencies点击 + -> Jar -> From modules with dependencies
  • I select the main class as the interface suggests我选择主类作为界面建议
  • Option 'Extract to target Jar'选项“提取到目标 Jar”
  • I currently have the META-INF/MANIFEST.MF Manifest directory set at src/main (the full path)我目前在src/main (完整路径)中设置了META-INF/MANIFEST.MF Manifest 目录
  • In the following window, on the left I see all the dependencies of the project.在接下来的窗口中,我会在左侧看到项目的所有依赖项。 At the bottom of the list, it says myPackage compile output在列表的底部,它说myPackage compile output

After applying and oking, I generate the jar file to see, as I would expect, that inside there is both the META-INF/MANIFEST.MF and the myPackage/myClass in the jar root, along with a all the other dependencies.在应用和确定之后,我生成了 jar 文件,正如我所期望的那样,在 jar 根目录中有META-INF/MANIFEST.MFmyPackage/myClass ,以及所有其他依赖项。 However when I try to run the jar either by:但是,当我尝试通过以下方式运行 jar 时:

  • cd to the location of the jar and use java -jar myGeneratedJarFilename.jar cd到 jar 的位置并使用java -jar myGeneratedJarFilename.jar
  • Use the intellij idea run/debug configuration with the 'application' or the 'run jar application'将 intellij idea 运行/调试配置与“应用程序”或“运行 jar 应用程序”一起使用

The result is the same:结果是一样的:

no main manifest attribute, in myGeneratedJarFilename.jar在 myGeneratedJarFilename.jar 中没有主要清单属性

I've tried using the invalidate caches and restart several times.我试过使用无效缓存并重新启动几次。 I've read several sets of documentation regarding Jars, executable Jars and the Java classpath but I can't find an error.我已经阅读了几组关于 Jars、可执行 Jars 和 Java 类路径的文档,但我找不到错误。

  • I am using Intellij Idea Community 2020.2 , on Arch Linux.我在 Arch Linux 上使用Intellij Idea Community 2020.2
  • Upon typing java -version on the terminal I see openjdk version "14.0.2" 2020-07-14 .在终端上输入java -version ,我看到openjdk version "14.0.2" 2020-07-14
  • THe Class-Path in the manifest file is a bit of a mystery to me.清单文件中的类路径对我来说有点神秘。 The docs say that it is a relative path but relative to what I'm not sure.文档说这是一个相对路径,但相对于我不确定的路径。 Any clarifications on that from a pro would be nice!专业人士对此的任何澄清都会很好!
  • The project structure was made using the org.apache.maven quickstark archetype项目结构是使用 org.apache.maven quickstark 原型制作的

Can anyone offer any ideas as to what may be the problem?任何人都可以就可能出现的问题提供任何想法吗?

Thanks a lot in advance!非常感谢!

The problem was that I was using the InteliJ Idea build process to build a fat jar, which is not supported in IntelliJ Idea.问题是我使用 InteliJ Idea 构建过程来构建一个胖 jar,IntelliJ Idea 不支持它。

To get it working, I updated my pom file:为了让它工作,我更新了我的 pom 文件:

<?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>myPackage</groupId>
    <artifactId>myClass</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>myClass</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <repositories>
        <repository>
            <id>hortonworks</id>
            <name>hortonworks</name>
            <url>https://repo.hortonworks.com/content/repositories/releases/</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
            <groupId>org.apache.hadoop</groupId>
            <artifactId>hadoop-common</artifactId>
            <version>2.7.3.2.6.2.0-205</version>
        </dependency>
        <dependency>
            <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
            <groupId>org.apache.hive</groupId>
            <artifactId>hive-exec</artifactId>
            <version>1.2.1000.2.6.2.69-1</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>myPackage.myClass</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Then I had to open the Maven window (View-> Tool Windows -> Maven) and use the Maven tasks clean then compile then package and the jars were created as expected.然后我必须打开 Maven 窗口(视图-> 工具窗口-> Maven)并使用 Maven 任务clean然后compile然后package ,并按预期创建 jars。

Note:笔记:

  • In my working structure, the MANIFEST.MF is at src/main/resources/META-INF/MANIFEST.MF .在我的工作结构中, MANIFEST.MF 位于src/main/resources/META-INF/MANIFEST.MF
  • I did not use any of the IntelliJ project structure Artifacts actions to get the working fat jars.我没有使用任何 IntelliJ 项目结构 Artifacts 操作来获取工作的胖罐子。 These aren't using Maven, even if you created a Maven project.即使您创建了 Maven 项目,这些也不使用 Maven。

Thanks to y.bedrov for the definitive answer when lead to me understanding why it wasn't working.感谢 y.bedrov 在让我理解为什么它不起作用时的明确答案。

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

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