2

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>
Nick
  • 29
  • 1
  • 3
  • 1
    this is too vague. Is your manifest file correct? Is there a main method to start off? is Java configured? ... – Stultuske Mar 31 '16 at 13:51
  • 1
    moreover, which command are you using to run the jar? You probably need a fat jar otherwise dependencies would not be included and your main would likely fail (hence look for the Maven Shade Plugin). Last but not least, junit dependency should be in test scope. – A_Di-Matteo Mar 31 '16 at 13:53
  • I think manifest is correct.Should i configurate Java in pom file? – Nick Mar 31 '16 at 13:55
  • check [this SO post](http://stackoverflow.com/questions/15869784/how-to-run-a-maven-created-jar-file-using-just-the-command-line) for further hints – A_Di-Matteo Mar 31 '16 at 14:03

1 Answers1

1

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
jaydez
  • 153
  • 8