I tried to do it through IntelliJ IDEA and Maven on my own. To no avail ... With the standard tools, the idea could not be done, since JDK 11 removed the module from JavaFX, respectively, the packer for it too. As a result, began to torment Maven. Here is my pom.xml:

<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mavenproject</groupId> <artifactId>MavenProject</artifactId> <packaging>jar</packaging> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>11.0.1</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>11.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>Main</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>11</source> <target>11</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>Main</mainClass> </configuration> </plugin> </plugins> </build> </project> 

I compile with the command:

 compile exec:java 

I tried to create a jar through:

 clean package assembly:single 
  • The easiest way is through maven. Specify in more detail what you did and what did not work? - Anton M
  • Plz Wright Rashshan - Anton Sorokin
  • Several options are here: baeldung.com/executable-jar-with-maven - Sergi
  • I have the same problem. Tell me, what team are you creating a jar? (mvn package?) In your sheet, for one plug-in, Main is set to mainClass, and for another, is MainLauncher a typo? - Peter Leskov
  • @ PetrLeskov can use mvn package. The Main class is specified as mainClass in the compiler plugin (in theory, you can also specify the MainLauncher class). MainLauncher must be specified in the plugins relating directly to the creation of the jar. Also watch the video indicated at the beginning of my answer, there should be all the answers. - Vladislav Hukovskii

2 answers 2

In IntelIJ using the CTRL + ALT + SHIFT + S key combination we get into the project structure. Go to the Artifacts point. Click on Add (green plus), select JAR and select from modules with dependencies. In the pop-up window, select the main class. Click OK, close the window. Another window pops up, in which we click Apply, we close this window. Setup is complete. Go to the Build item. Choose Build Artifact, then Build.

The executable file will be generated in the out / Artifacts directory. Problems may arise if the project is importing any package. but this import is not in the classpath. If the JAR does not start, go to the project structure, select Modules -> Dependencies -> Add -> Project Libration -> Attach JAR

If suddenly your console application, and you want to launch it not through the console, you will need to create a bat file in which to register the following command

 java -jar filename.jar 

And run already file .bat.

Try adding the following code to pom.xml (Modify for yourself)

  <build> <plugins> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <archive> <manifest> <mainClass>полный.путь.к.главному.классу</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> 

  • I have already tried it in the manner described, nothing happens when the jar is started. I suppose earlier (until JavaFX was removed in Java 11) this was easily done via javafxpackager. Now, if you choose to choose Artifacts-> JavaFx Application, the assembly gives an error. Now looking for alternative ways. - Vladislav Hukovskii
  • @VladGukovsky Added another way. - Konstantin_SH
  • Check in the manifest of your jar-nick whether Main-Class is specified: - alex
  • @Konstantin_SH Did not work. I posted my pom.xml, and the way I tried to run. - Vladislav Hukovskii
  • @VladislavHookovskii Here <mainClass> Main </ mainClass> You need to specify the full path to the file with the main class. You just have the name of the main class ... Just in case, check the Build on make checkbox in the window where Apply should be clicked - Konstantin_SH

And a month has not passed, I finally found the answer. Everything is set out in this video. This file is created using the following plugin:

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

It should be noted that the launcher must be specified in the tag, and not the main class itself. Suppose our main class is Main. Then the launcher code looks like this:

 public class MainLauncher { public static void main(String[] args) { Main.main(args); } } 

This is a very important point, because without this, the jar does not want to start (in fact, I had a problem with this, because I tried this plugin more than once). Then we launch our jarnik either via java -jar, or with a regular double click.

Here is my pom.xml:

 <?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>com.vladhuk</groupId> <artifactId>MavenProject</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-controls</artifactId> <version>11.0.1</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-graphics</artifactId> <version>11.0.1</version> </dependency> <dependency> <groupId>org.openjfx</groupId> <artifactId>javafx-fxml</artifactId> <version>11.0.1</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>11</source> <target>11</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <version>1.2.1</version> <executions> <execution> <goals> <goal>java</goal> </goals> </execution> </executions> <configuration> <mainClass>Main</mainClass> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> <configuration> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <mainClass>MainLauncher</mainClass> </transformer> </transformers> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>