Reputation: 3807
I've looked at the other stackoverflow questions regarding this issue and those questions and answers relate to the improper main class setting in the pom.xml. I've written the main class to have the correct package name and proper case sensitivity but whenever i run my jar I get the error of Could not find or load main class
.
I added the project to github: https://github.com/quicksilversly/maze and my maven jar plugin configuration looks like this:
configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.willisjtc.maze.VertxStarter</mainClass>
</manifest>
</archive>
</configuration>
And here is my main class:
package com.willisjtc.maze;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class VertxStarter extends AbstractVerticle {
private static final Logger logger = LogManager.getLogger(VertxStarter.class);
public static void main(String... args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle("ruby/webVerticle.rb");
vertx.deployVerticle("ruby/mazeVerticle.rb");
}
}
This is what I am getting when I run java tvf target/jar-to-run.jar
:
597 Sat Aug 06 21:59:54 MDT 2016 META-INF/MANIFEST.MF
0 Sat Aug 06 21:59:54 MDT 2016 META-INF/
0 Sat Aug 06 21:43:12 MDT 2016 com/
0 Sat Aug 06 21:43:12 MDT 2016 com/willisjtc/
0 Sat Aug 06 21:43:12 MDT 2016 com/willisjtc/maze/
0 Sat Aug 06 21:43:18 MDT 2016 META-INF/maven/
0 Sat Aug 06 21:43:18 MDT 2016 META-INF/maven/com.willisjtc/
0 Sat Aug 06 21:43:18 MDT 2016 META-INF/maven/com.willisjtc/maze/
0 Sat Aug 06 21:43:10 MDT 2016 ruby/
907 Sat Aug 06 21:43:12 MDT 2016 com/willisjtc/maze/VertxStarter.class
220 Sat Aug 06 21:56:14 MDT 2016 META-INF/maven/com.willisjtc/maze/pom.properties
2105 Sat Aug 06 21:56:14 MDT 2016 META-INF/maven/com.willisjtc/maze/pom.xml
137 Sat Aug 06 21:43:10 MDT 2016 ruby/mazeVerticle.rb
221 Sat Aug 06 21:43:10 MDT 2016 ruby/webVerticle.rb
So my main class is there as expected.
What do i need to change in order to run my jar file?
Upvotes: 3
Views: 6292
Reputation: 7353
This has worked perfectly for me.
Add these plugins to your pom.xml
:
<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-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<!--enter the name of your class here-->
<mainClass>YourPackage.YourMainClass</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Make sure to change the mainClass
element to the correct package and class name.
Then run mvn package
to build the jar executable.
The jar will be built inside of the /target
directory.
Then you can run the jar from the command line with this command:
java -jar <NameOfJarFile>.jar
Upvotes: 3
Reputation: 2791
When you run java -jar output-jar-file.jar
, your main()
can not find external packages like io.vertx.core.AbstractVerticle
, io.vertx.core.Vertx
, thus can not be loaded.
By using <addClasspath>true</addClasspath>
, java will expect all the dependencies are presented together with your runnable jar in the same dir. In fact, all your dependencies are inside .m2
dir.
You need to copy all dependencies to target
dir and declare it as a classpath for your output-jar-file. Change your pom.xml like this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>com.willisjtc.maze.VertxStarter</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
Upvotes: 0