Executar jar com JavaFX com dois cliques compilado com maven

29/10/2018

6

Olá galera, estou tentando executar um jar com dois cliques, ele tem uma janela com JavaFX 11 compilado com maven, JDK 11.

ele compila e roda com o comando: mvn compile exec:java, porém eu quero que ele rode através do jar com dois cliques.

eu o compilo com:
(1) mvn install
ele gera o jar e eu consigo executar com o sequinte comando:
(2) java --module-path="C:\\\\Program Files\\\\javafx-sdk-11\\\\lib" --add-modules=javafx.controls -cp javafx-0.0.1-SNAPSHOT-jar-with-dependencies.jar br.com.teste.javafx.HelloFX

porém quero executar-lo com dois cliques
tentei colocar o sequinte linha no manifest,
(3) Class-Path: C:\\\\Program Files\\\\javafx-sdk-11\\\\lib\\\\javafx.controls.jar C:\\\\Program Files\\\\javafx-sdk-11\\\\lib\\\\javafx.base.jar C:\\\\Program Files\\\\javafx-sdk-11\\\\lib\\\\javafx.graphics.jar
porém dai não funciona nem mais com o comando (2).

O pom que estou usando é o seguinte:

<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>br.com.teste</groupId>
	<artifactId>javafx</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>testejfx</name>

	<url>http://maven.apache.org</url>
	
	<pluginRepositories>
		<pluginRepository>
			<id>arnaudroger-maven-plugin-repository</id>
			<url>https://arnaudroger.github.io/maven</url>
		</pluginRepository>
	</pluginRepositories>

	<dependencies>
		<dependency>
			<groupId>org.openjfx</groupId>
			<artifactId>javafx-controls</artifactId>
			<version>11</version>
		</dependency>
	</dependencies>
	
	<build>
		<plugins>
		<plugin>
			<groupId>com.zenjava</groupId>
			<artifactId>javafx-maven-plugin</artifactId>
			<version>8.9.0-SNAPSHOT</version>
			<configuration>
				<mainClass>br.com.teste.javafx.HelloFX</mainClass>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.8.0</version>
			<configuration>
				<release>11</release>
				<fork>true</fork>
				<!--<compilerArgs>
					<arg>-J-XaddExports:jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED</arg>
					<arg>-J-XaddExports:jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED</arg>
				</compilerArgs>-->
			</configuration>
			 <dependencies>
					<dependency>
						<groupId>org.ow2.asm</groupId>
						<artifactId>asm</artifactId>
						<version>6.0_ALPHA</version>
					</dependency>
				</dependencies>
		</plugin>
		<plugin>
			<groupId>org.codehaus.mojo</groupId>
			<artifactId>exec-maven-plugin</artifactId>
			<version>1.6.0</version>
			<executions>
				<execution>
					<goals>
						<goal>java</goal>
					</goals>
				</execution>
			</executions>
			<configuration>
				<mainClass>br.com.teste.javafx.HelloFX</mainClass>
			</configuration>
		</plugin>
	    <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-shade-plugin</artifactId>
			<version>3.2.0</version>
			<executions>
			  <execution>
				<phase>package</phase>
				<goals>
				  <goal>shade</goal>
				</goals>
				<configuration>
				  <transformers>
					<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
					  <mainClass>br.com.teste.javafx.HelloFX</mainClass>
					</transformer>
				  </transformers>
				</configuration>
			  </execution>
			</executions>
        </plugin>
	    <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<configuration>
				<archive>
					<manifest>
						<addClasspath>true</addClasspath>
						<classpathPrefix>libs/</classpathPrefix>
						<mainClass>
							br.com.teste.javafx.HelloFX
						</mainClass>
					</manifest>
				</archive>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<configuration>
				<outputDirectory>${project.build.directory}/../../target/modules</outputDirectory>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-dependency-plugin</artifactId>
			<executions>
				<execution>
					<id>copy-dependencies</id>
					<phase>package</phase>
					<goals>
						<goal>copy-dependencies</goal>
					</goals>
					<configuration>
						<outputDirectory>${project.build.directory}/../../target/modules</outputDirectory>
						<overWriteReleases>false</overWriteReleases>
						<overWriteSnapshots>false</overWriteSnapshots>
						<overWriteIfNewer>true</overWriteIfNewer>
					</configuration>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-assembly-plugin</artifactId>
			<executions>
				<execution>
					<phase>package</phase>
					<goals>
						<goal>single</goal>
					</goals>
					<configuration>
						<archive>
						<manifest>
							<mainClass>
								br.com.teste.javafx.HelloFX
							</mainClass>
						</manifest>
						</archive>
						<descriptorRefs>
							<descriptorRef>jar-with-dependencies</descriptorRef>
						</descriptorRefs>
					</configuration>
				</execution>
			</executions>
		</plugin>
		<plugin>
			<groupId>com.jolira</groupId>
			<artifactId>onejar-maven-plugin</artifactId>
			<executions>
				<execution>
					<configuration>
						<mainClass>org.baeldung.executable.
						  ExecutableMavenJar</mainClass>
						<attachToBuild>true</attachToBuild>
						<filename>
						  ${project.build.finalName}.${project.packaging}
						</filename>
					</configuration>
					<goals>
						<goal>one-jar</goal>
					</goals>
				</execution>
			</executions>
		</plugin>
        <plugin>
            <groupId>com.zenjava</groupId>
            <artifactId>javafx-maven-plugin</artifactId>
            <version>8.8.3</version>
            <configuration>
                <mainClass>br.com.teste.javafx.HelloFX</mainClass>
            </configuration>
        </plugin>		
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-plugin</artifactId>
			<configuration>
				<argLine>-XaddExports:java.base/sun.nio.ch=ALL-UNNAMED -XaddExports:jdk.unsupported/sun.misc=ALL-UNNAMED</argLine>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.felix</groupId>
			<artifactId>maven-bundle-plugin</artifactId>
			<extensions>true</extensions>
			<version>3.2.0</version>
			<configuration>
				<instructions>
					<_noee>true</_noee>
					<_failok>true</_failok>
				</instructions>
			</configuration>
		</plugin>
		</plugins>
	</build>
</project>


A classe br.com.teste.javafx.HelloFX está em: https://openjfx.io/openjfx-docs/HelloFX.java
basta adicionar: package br.com.teste.javafx;

O module-info.java

module br.com.teste.javafx{
	requires javafx.base;
        requires javafx.graphics;
	requires javafx.controls;
	exports br.com.teste.javafx;
}


localizado em:

─src
│ ├───main
│ │ ├───java
│ │ │ │ module-info.java
│ │ │ │
│ │ │ └───br
│ │ │ └───com
│ │ │ └───teste
│ │ │ └───javafx
│ │ │ HelloFX.java



Gostaria de saber também se estou compilando certo o projeto no formato JigSaw - modular, para poder usar os módulos do JavaFX 11?

Desde já Agradeço!
Responder

Post mais votado

07/11/2018

Opa, eu conheço duas formas:

1- você pode criar um .bat que executa o jar. Ex:

java arquivo.jar


e salvar o arquivo com extensão .bat.

2- Existe o programa "Launch4j" que converte o arquivo .jar para .exe, que executa direto no windows. Site:

http://launch4j.sourceforge.net/
Responder
×
+1 DevUP
Acesso diário, +1 DevUP
Parabéns, você está investindo na sua carreira