I have problem after build project on maven in jar.
I make package and create .jar file:
mvn package
after i run my jar file
java -jar artifactId.jar
and get error:
Error: Could not find or load main class com.emercit.app.App
If i run class in target directory, i get similar error:
cd target/classes/com/emercit/app
java App
Error: Could not find or load main class App
My code in App class:
package com.emercit.app;
public class App extends Application {
/**
Program code
**/
}
public static void main (String[] args) {
//Init form
Thread myThready = new Thread(() -> {
launch(args);
});
myThready.start();
}
}
My build properties in pom.xml:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>false</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.emercit.app.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>install</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Build your jar with this plugin and specify the main class, this will add the correct META-INF in your generated jar.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>com.barclaycard.blackboxtester.Application</Main-Class>
<Build-Number>123</Build-Number>
</manifestEntries>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
Your setup is similar to how I do it except you need to change this parameter in your POM file to be a value of "true".
<addClasspath>true</addClasspath>
Without it you would have to include dependencies on the command line and you are not. Java won't find jars just because they are in a directory. This command will add a "classpath" in the MANIFEST.MF file withing the JAR.
The generated MANIFEST.MF file would contain a line something like this (taken from one of my opensource projects). Your jar dependencies would of course be different.
Class-Path: calendar-2.2.3.jar algebrain-2.2.3.jar commons-codec-1.9.j
ar argument-4.3.8.jar slf4j-api-1.7.13.jar slf4j-log4j12-1.7.13.jar l
og4j-1.2.17.jar
创建任何 mavenized 项目时的文件结构应该src/main/java和包应该包含在类文件中,否则它将抛出错误无法找到或加载主类
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.