Fat JAR无法运行。出现“没有主清单属性”错误。尝试过POM文件,仍然失败。

huangapple go评论69阅读模式
英文:

Fat JAR not working. "No Main Manifest Attribute". Tried POM file, still failing

问题

以下是您提供的内容的翻译:

我有一个使用Maven的Netbeans项目,我正在尝试将其编译成可执行的JAR文件。我认为到目前为止,在Netbeans内部一切都正常工作,但是当我打包它时,它只是一个20KB的文件-SNAPSHOT,我无法使其运行起来。
有人向我指出了一些MAVEN代码,可以打包所有依赖项并将其制作成一个“FAT JAR”文件。我照做了,并开始出现错误“没有主清单属性”。我复制了一些更多的MAVEN代码片段来添加主清单,然而我仍然得到相同的错误“没有主清单”,因此我认为我做错了什么。这是我的POM.XML文件:

<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>com.MyCompany</groupId>
    <artifactId>Client_BoxTest</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.fazecast</groupId>
            <artifactId>jSerialComm</artifactId>
            <version>[2.0.0,3.0.0)</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>14</maven.compiler.source>
        <maven.compiler.target>14</maven.compiler.target>
    </properties>
    <build>
        <finalName>Client-Boxtest</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <!-- Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>my.client_boxtest.BoxTestUI</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <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>

现在,我不确定我的主类的位置:这是一个相当简单的程序,我的NetBeans中的树状结构看起来像这样,只有一个文件:

Fat JAR无法运行。出现“没有主清单属性”错误。尝试过POM文件,仍然失败。

英文:

I have a Netbeans project with Maven that I'm trying to compile into an executable JAR file. I think so far everything works fine inside Netbeans however when I package it was a 20kb file-SNAPSHOT and couldnt get it to run.
Someone pointed me to some MAVEN code to package all dependencies and make it into a "FAT JAR" file. I did that and started getting an error "No Main Manifest Attribute" I copied some more MAVEN snippets to add the main manifest however I still get the same error "No Main Manifest" so I assume I did something wrong. Here's my POM.XML file:

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;com.MyCompany&lt;/groupId&gt;
&lt;artifactId&gt;Client_BoxTest&lt;/artifactId&gt;
&lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
&lt;packaging&gt;jar&lt;/packaging&gt;
&lt;dependencies&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.fazecast&lt;/groupId&gt;
&lt;artifactId&gt;jSerialComm&lt;/artifactId&gt;
&lt;version&gt;[2.0.0,3.0.0)&lt;/version&gt;
&lt;/dependency&gt;
&lt;/dependencies&gt;
&lt;properties&gt;
&lt;project.build.sourceEncoding&gt;UTF-8&lt;/project.build.sourceEncoding&gt;
&lt;maven.compiler.source&gt;14&lt;/maven.compiler.source&gt;
&lt;maven.compiler.target&gt;14&lt;/maven.compiler.target&gt;
&lt;/properties&gt;
&lt;build&gt;
&lt;finalName&gt;Client-Boxtest&lt;/finalName&gt;
&lt;plugins&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
&lt;version&gt;3.6.1&lt;/version&gt;
&lt;configuration&gt;
&lt;source&gt;1.8&lt;/source&gt;
&lt;target&gt;1.8&lt;/target&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;!-- Build an executable JAR --&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;
&lt;version&gt;3.1.1&lt;/version&gt;
&lt;configuration&gt;
&lt;archive&gt;
&lt;manifest&gt;
&lt;addClasspath&gt;true&lt;/addClasspath&gt;
&lt;classpathPrefix&gt;lib/&lt;/classpathPrefix&gt;
&lt;mainClass&gt;my.client_boxtest.BoxTestUI&lt;/mainClass&gt;
&lt;/manifest&gt;
&lt;/archive&gt;
&lt;/configuration&gt;
&lt;/plugin&gt;
&lt;plugin&gt;
&lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
&lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
&lt;version&gt;3.1.1&lt;/version&gt;
&lt;configuration&gt;
&lt;descriptorRefs&gt;
&lt;descriptorRef&gt;jar-with-dependencies&lt;/descriptorRef&gt;
&lt;/descriptorRefs&gt;
&lt;/configuration&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;id&gt;make-assembly&lt;/id&gt;
&lt;phase&gt;package&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;single&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;/project&gt;

Now I am not sure about the location of my main class: it's a fairly simple program and my tree in NetBeans looks like this, with only one file:

Fat JAR无法运行。出现“没有主清单属性”错误。尝试过POM文件,仍然失败。

答案1

得分: 2

尝试根据下面的描述在你的POM中添加对主类的引用,这对我起作用:

<project>
[...]
<build>
[...]
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
[...]
<archive>
<manifest>
<mainClass>org.sample.App</mainClass>
</manifest>
</archive>
</configuration>
[...]
</plugin>
[...]
</plugins>
</project>
英文:

Try to add a reference to the main class in your POM as described below, it works for me:

&lt;project&gt;
[...]
&lt;build&gt;
[...]
&lt;plugins&gt;
&lt;plugin&gt;
&lt;artifactId&gt;maven-assembly-plugin&lt;/artifactId&gt;
&lt;version&gt;3.3.0&lt;/version&gt;
&lt;configuration&gt;
[...]
&lt;archive&gt;
&lt;manifest&gt;
&lt;mainClass&gt;org.sample.App&lt;/mainClass&gt;
&lt;/manifest&gt;
&lt;/archive&gt;
&lt;/configuration&gt;
[...]
&lt;/plugin&gt;
[...]
&lt;/project&gt;

答案2

得分: 1

maven-shade-plugin非常强大,用于创建Uber jar或者fat jar,这已经是十多年来执行此任务的首选插件。链接https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html详细描述了如何指定mainClass。

英文:

The maven-shade-plugin is much more powerful for create an Uber jar or fat jar and is the preferred plugin for this task for over ten years.
https://maven.apache.org/plugins/maven-shade-plugin/examples/executable-jar.html describes how you should specify the mainClass.

huangapple
  • 本文由 发表于 2020年9月23日 00:08:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64013607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定