Reputation: 29
I'm new to Maven. I created Maven Project and installed it (build succesful),but i cant run jar file from command prompt, it says
Error: Unable to access jarfile
I think i've got mistake in POM 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>ru.nick.kru</groupId>
<artifactId>ParagonCase</artifactId>
<version>1.1</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>Main</mainClass>
<packageName>ru.nick.kru</packageName>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
</project>
Upvotes: 2
Views: 7291
Reputation: 153
Building an executable Jar file with Maven can be tricky, but there is a way to do it! It looks like you're on the right track, but might be missing "ParagonCase" in the "packageName" property of the plugin (if your Main function is ru.nick.kru.ParagonCase.Main, then your packageName should be ru.nick.kru.ParagonCase).
I've also found it useful to use the maven shade plugin, which bundles all the necessary dependencies inside the Jar (i.e. you don't need all those dependencies as separate Jars in your classpath when you run the Jar).
In your POM you could add the following build plugin:
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<!-- add Main-Class to manifest file -->
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>ru.nick.kru.ParagonCase.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</build>
In order to run the Jar (after it is created with mvn package
). Launch it with java:
java -jar ParagonCase.jar
Upvotes: 1