How to generate an executable . jar using Maven?

Asked

Viewed 16,809 times

15

Is it necessary to make some extra configuration to generate a Maven executable . jar? How have I never used this technology under development desktop I’m a little lost.

When I build the project (the logs show that you built successfully) and try to run it, nothing happens. To see if there was an exception or something similar, I tried to execute by command line and here’s the message:

Unable to locate or load main class


imagem :)

My file pom.xml is like this:

<?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>kitty.project</groupId>
    <artifactId>Kitty</artifactId>
    <version>1.0</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>
    <name>Kitty</name>
    <dependencies>
       <!-- Dependências do projeto -->
    </dependencies>
</project>

Is any configuration missing? How can I fix this and generate one .jar executable as if it were a normal Java application?

3 answers

18


Yes, it is missing to specify your main class (main class). For this you can use this plugin:

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>sua.classe.Main</mainClass>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>
  • It worked, even complaining about the lack of <version>. Can some configuration be made in this plugin to generate a single . jar with everything packaged? Because it was generated several directories besides the "executable".

  • It’s forgotten about the <version>. As for the other directories files created this is normal. If you are versioning your project just configure your scm to ignore the target folder. In my case, how I use Git I create a file called ". gitignore" and inside it I put (among other things I want Git to ignore), the folder name "target".

11

To reply from Afonso worked as I expected and generated a .jar that runs my application.

Until then I didn’t know they existed plugins for Maven and, researching a little more found the Maven Assembly Plugin that (in addition to doing the same thing) still allows building a single .jar with all dependencies packaged.

In the case of my application, this seemed more interesting, in the sample page there are some other situations where the plugin can be useful. The settings I used in my file pom.xml were:

<plugins>

   <!-- ... -->

   <plugin>
       <artifactId>maven-assembly-plugin</artifactId>
       <configuration>
          <archive>
              <manifest>
                  <mainClass>br.com.MinhaClasseMain</mainClass>
              </manifest>
          </archive>
          <descriptorRefs>
              <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
       </configuration>
       <executions>
          <execution>
              <phase>package</phase>
              <goals>
                 <goal>single</goal>
              </goals>
          </execution>
       </executions>
   </plugin>

   <!-- ... -->

</plugins>

Note: it is possible to omit the artifact group since Maven has the org.apache.maven.plugins as standard group. And in this case, it is the same group as the plugin.

  • 1

    Wow, thank you, I already fell exactly into the problem of dependencies, I came back and saw your answer.

6

Renan also do not know such technology either, but about Java (and not Maven) if I am not mistaken the command java search for a file .class, is not?

To run files jar would need the parameter -jar, as in the example:

java -jar Kitty-1.0.jar
  • 1

    Now appeared "no major manifest attribute in Kitty-1.0. jar". It must be something in the pom.xml but for not knowing the technology I do not know how to fix.

  • 1

    @re22 I think the problem is in the netbeans generated manifest, if opening the jar by Winrar has a file called Manifest.mf, what content it contains?

  • 1

    Has this here

  • 1

    @re22 as the image the class name was Main in the manifest, but in . java should be something else and this caused the conflict, yes it seems that the solution of @Afonso seems correct, I could not test.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.