英文:
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中的树状结构看起来像这样,只有一个文件:
英文:
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:
<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>
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:
答案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:
<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>
[...]
</project>
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论