1

I try to generate an executable jar file from a project which use : Maven, JFX, JFoenix, Hibernate ... in IntelliJ

I have tried many solution which we can found in different developer forum/website (StackOverflow included). Therefore no one works for me..

I have tried to create the executable jar file with :

  • Command : mvn compile _ which create two executable jar which execute nothing..

  • Create an artifact : File -> Save All -> Project Structure -> + -> Jar -> From modules dependacies -> Main Class : MyMainClass / Directory : src\main\java OR src\main\resources (because he worked for someone to change this.) -> Ok
    Build -> Build artifact -> Build OR Rebuild. At this moment i have an executable, but it execute nothing..

  • Shade plugging : (You'll see in the code of pom.xml) which give in result :
Failed to execute goal
org.apache.maven.plugins:maven-shade-plugin:3.2.1:shade (default-cli)
on project GymFormManager: Failed to create shaded artifact, project main
artifact does not exist.

pom.xml, i take only the important part because it's too huge:

...

<modelVersion>4.0.0</modelVersion>
<groupId>com.gymform.maven</groupId>
<artifactId>GymFormManager</artifactId>
<version>1.0-SNAPSHOT</version>

</dependencies>
    ...   
    <!-- Try To Create Executable Jar -->
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.1</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.plexus</groupId>
        <artifactId>plexus-utils</artifactId>
        <version>1.1</version>
    </dependency>
</dependencies>

<build>
    <plugins>

        ...

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <mainClass>com.gymformmanager.Main</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.2.1</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>com.gymformmanager.Main</mainClass>
                            </transformer>
                        </transformers>

              <!-- I have tried with and without hadedArtifact -->       <shadedArtifactAttached>true</shadedArtifactAttached>
                        <shadedClassifierName>GymFormManager</shadedClassifierName> <!-- Any name that makes sense -->
                    </configuration>
                </execution>
            </executions>
        </plugin>

Currently, i have three executable jar which doing nothing :

The first executable The path of the first executable

The second and third executable Their paths

Edit: After following the advices of Jony, i have this error :

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/gymformmanager/Main has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runt
ime only recognizes class file versions up to 52.0
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

I resolved the problem of version with this : Windows ignores JAVA_HOME: how to set JDK as default?

Error: JavaFX runtime components are missing, and are required to run this application

While i have in my pom.xml the dependency :

 <dependency>
        <groupId>org.openjfx</groupId>
        <artifactId>javafx-controls</artifactId>
        <version>11</version>
 </dependency>

3 Answers3

2

You can try to assemble a so called fatjar with maven:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>
        <archive>
            <manifest>
                <mainClass>com.gymformmanager.Main</mainClass>
            </manifest>
        </archive>
    </configuration>
    <executions>
        <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
 </plugin>

This will create an additional jar file in your output directory called GymFormManager-jar-with-dependencies.jar. You can run it with:

java -jar GymFormManager-jar-with-dependencies.jar
Jony
  • 1,764
  • 2
  • 12
  • 15
  • Thanks for your answer, therefore i have this error now: no main manifest attribute in GymFormManager-1.0-SNAPSHOT-jar-with-dependencies.jar – Samy T. Bencharef Feb 15 '19 at 14:10
  • The main manifest attribute refers to your mainClass, do you have a Class-File at com.gymformmanager.Main with a "public static void main(String[] args)" method? – Jony Feb 15 '19 at 14:13
  • Yes i have it ... And when i use exec-maven-plugin it is executed. – Samy T. Bencharef Feb 15 '19 at 14:17
  • Sorry, i mixed where you have to put the mainClass... I updated my answer, could you try again with the new plugin config? – Jony Feb 15 '19 at 14:28
  • Thanks a lot to take time for me ! Therefore, i have another error now, i have edited my post .. – Samy T. Bencharef Feb 15 '19 at 14:39
  • No problem. Your new error is basically saying that your jar was compiled to Java 11 but you are trying to run it with Java 9. You can run mvn --version and java --version and try to see if the Java Versions match, this happens when you have multiple JDKs installed – Jony Feb 15 '19 at 14:45
  • I have add my maven and java version is the post, they're not the same (I thinks).Should i change my environment variable? – Samy T. Bencharef Feb 15 '19 at 14:57
  • You could either change your JAVA_HOME Environment Variable or tell maven to compile to a different Java Version (Java 8) in your case. You can check this link for more details on the latter: https://maven.apache.org/plugins/maven-compiler-plugin/examples/set-compiler-source-and-target.html – Jony Feb 15 '19 at 15:02
  • Now i have this error : Error: JavaFX runtime components are missing, and are required to run this application But the application is working perfetly when i execute it with intelliJ .. – Samy T. Bencharef Feb 15 '19 at 16:36
  • Not sure, but apparently the JavaFX runtime is not part of the JDK anymore when using Java 11, which would mean you have to add an extra dependency to your pom file. This one might work, but I an not sure: https://mvnrepository.com/artifact/org.openjfx/javafx/11.0.2 – Jony Feb 15 '19 at 18:40
  • With the upgrade of the javafx 11 to 11.0.2, i still have the same issue.. – Samy T. Bencharef Feb 15 '19 at 19:21
  • I am not really sure when it comes to JavaFX, haven't really used it. Apparently you have to have a non JavaFX Main Class which calls a JavaFX Application, see here: https://github.com/javafxports/openjdk-jfx/issues/236#issuecomment-426606561 but as I said, I am not really familiar with JavaFX, so I don't think I'm of much use with this issue. – Jony Feb 15 '19 at 19:37
  • Thanks a lot, that was one of the last things, i get few issues after but i resolved them ! A big big big thanks you jony ! – Samy T. Bencharef Feb 15 '19 at 23:17
0

If you want to create an executable jar file, you need to configure Maven Archiver accordingly. You need to tell it which main class to use. This is done with the configuration element. Here is a sample pom.xml configured to add the classpath and use the class fully.qualified.MainClass as the main class:

<project>
  ...
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-jar-plugin</artifactId>
        ...
        <configuration>
          <archive>
            <manifest>
              <addClasspath>true</addClasspath>
              <mainClass>fully.qualified.MainClass</mainClass>
            </manifest>
          </archive>
        </configuration>
        ...
      </plugin>
    </plugins>
  </build>
  ...
  <dependencies>
    <dependency>
      <groupId>commons-lang</groupId>
      <artifactId>commons-lang</artifactId>
      <version>2.1</version>
    </dependency>
    <dependency>
      <groupId>org.codehaus.plexus</groupId>
      <artifactId>plexus-utils</artifactId>
      <version>1.1</version>
    </dependency>
  </dependencies>
  ...
</project>
bessam Sahli
  • 105
  • 7
0

I guess javafx-maven-plugin is a good option to build JFX application. It is based on Java Packager Tool and can build both native bundle and jar. Call mvn jfx:jar to start building.

<plugin>
    <!-- Generate Executable JFX JAR -->
    <groupId>com.zenjava</groupId>
    <artifactId>javafx-maven-plugin</artifactId>
    <version>8.8.3</version>
    <configuration>
        <vendor>${vendor}</vendor>
        <mainClass>${mainClass}</mainClass>
        <allPermissions>true</allPermissions>
        <manifestAttributes>
            <Implementation-Title>${project.name}</Implementation-Title>
            <Implementation-Version>${project.version}</Implementation-Version>
            <Implementation-Vendor>${project.organization.name}</Implementation-Vendor>
            <Build-Timestamp-Format>${maven.build.timestamp.format}</Build-Timestamp-Format>
            <Build-Timestamp>${dev.build.timestamp}</Build-Timestamp>
        </manifestAttributes>
        <jfxMainAppJarName>${jfxMainAppJarName}</jfxMainAppJarName>
    </configuration>
    <executions>
        <execution>
            <id>create-jfxjar</id>
            <phase>package</phase>
            <goals>
                <goal>build-jar</goal>
            </goals>
        </execution>
    </executions>
</plugin>
Oleks
  • 1,011
  • 1
  • 14
  • 25