我有一个java程序,我刚刚按照一个教程,在Apache Netbeans 11中用Maven创建了.jar。但这个程序并没有执行......什么都没有发生。我甚至有一个运行它的.bat文件,但它显示:Windows can't find file NewMain wich is the main class。
这是我使用的maven。
<?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>com.mycompany</groupId>
<artifactId>mavenproject3</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<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>
<dependencies>
<dependency> <groupId>com.apple</groupId> <artifactId>AppleJavaExtensions</artifactId> <version>1.4</version> </dependency>
</dependencies>
<build>
<finalName>mavenproject3</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject3</artifactId>
<version>1.0-SNAPSHOT</version>
<configuration>
//Versión de JDK con la cual se ha construido el proyecto
// 1.8 significa que se utilizó Java8
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
//Sufijo que se le agregara al fichero JAR ejecutable
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
//Aqui se establece el nombre de la clase principal
<mainClass>com.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
是不是少了什么东西,或者放错了地方?
Manifest.MF
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Usuario
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_231
Main-Class: MainClass
MainClass不是主类
更新我已经做了修改
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: Usuario
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_231
Main-Class: NewMain
现在:"NewMain "父类未找到或加载。没有找到或加载NewMain父类
Jar文件应该有 Main-Class: name of the class containing the main (with package name)
中定义的参数 Manifest.mf
.
你必须按以下方式进行诊断。
java -jar <jar file name>
.Manifest.mf
文件。同样.bat文件是运行jar文件的辅助工具,它将有与第1点中提到的相同的命令。
包的名字必须放在前面。
<mainClass>com.mycompany.mavenproject3.NewMain</mainClass>
现在,它的工作原理是: