javafx-maven-plugin icon indicating copy to clipboard operation
javafx-maven-plugin copied to clipboard

Unable to run JavaFX via Maven

Open HenryDeLange opened this issue 5 years ago • 17 comments

I'm using the code provided at: https://github.com/openjfx/samples/tree/master/HelloFX/Maven

I did set the JAVA_HOME environment variable to point to Java 13.0.1.

However I get the following exception:

C:_dev\NetBeansProjects\hellofx>echo %JAVA_HOME% C:_dev\jdk-13.0.1 C:_dev\NetBeansProjects\hellofx>mvn -version Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: C:_tools\apache-maven-3.6.3\bin.. Java version: 13.0.1, vendor: Oracle Corporation, runtime: C:_dev\jdk-13.0.1 Default locale: en_ZA, platform encoding: Cp1252 OS name: "windows 10", version: "10.0", arch: "amd64", family: "windows"

C:_dev\NetBeansProjects\hellofx>mvn clean javafx:run [INFO] Scanning for projects... [INFO] [INFO] ------------------------< org.openjfx:hellofx >------------------------- [INFO] Building demo 1.0-SNAPSHOT [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ hellofx --- [INFO] Deleting C:_dev\NetBeansProjects\hellofx\target [INFO] [INFO] --- javafx-maven-plugin:0.0.3:run (default-cli) @ hellofx --- [INFO] Using 'UTF-8' encoding to copy filtered resources. [INFO] skip non existing resourceDirectory C:_dev\NetBeansProjects\hellofx\src\main\resources [INFO] Changes detected - recompiling the module! [INFO] Compiling 1 source file to C:_dev\NetBeansProjects\hellofx\target\classes Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. Unrecognized option: --module-path [ERROR] Command execution failed. org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404) at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166) at org.openjfx.JavaFXBaseMojo.executeCommandLine (JavaFXBaseMojo.java:491) at org.openjfx.JavaFXBaseMojo.executeCommandLine (JavaFXBaseMojo.java:453) at org.openjfx.JavaFXRunMojo.execute (JavaFXRunMojo.java:97) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156) at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957) at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289) at org.apache.maven.cli.MavenCli.main (MavenCli.java:193) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke (Method.java:567) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347) org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1) at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:404) at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:166) at org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:491) at org.openjfx.JavaFXBaseMojo.executeCommandLine(JavaFXBaseMojo.java:453) at org.openjfx.JavaFXRunMojo.execute(JavaFXRunMojo.java:97) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:156) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:148) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81) at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:305) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:957) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:289) at org.apache.maven.cli.MavenCli.main(MavenCli.java:193) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:567) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347) [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 7.226 s [INFO] Finished at: 2019-11-27T12:39:26+02:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.openjfx:javafx-maven-plugin:0.0.3:run (default-cli) on project hellofx: Error: Command execution failed. Process exited with an error: 1 (Exit value: 1) -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

I also tried a variety of things using the sample project provided by NetBeans, but couldn't get it running.

HenryDeLange avatar Nov 27 '19 10:11 HenryDeLange

Hi, I have been struggling since 5 days this --module -path problem. I found my solution. May be this is useful for you too.

  1. You need to add tags inside to your javafx-maven-plugin.

  2. After that give a java folder path to appropriate java version for your project.

They say maven reads your default jdk which is installed on your system. My default java version "1.8.0_151" but i use javaSE 11 for my project. So i pointed my java 11 path and javafx:run command and it successfully ran.

<plugin>
              
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.3</version>
                <configuration>
                    <mainClass>controller.Main</mainClass>
                    <executable>c:\Program Files\Java\jdk-11.0.1\bin\java</executable>
                </configuration>

 </plugin> 

tirnavali avatar Dec 30 '19 07:12 tirnavali

This should be fixed by my PR #47 already merged.

Basically the plugin did not use a JDK explicitly defined with the %JAVA_HOME% environment variable but preferred a JDK found in the %PATH%. That error shows up if the JDK found on the %PATH% is non modular.

In my case I was able to workaround it on the command line by redefining the PATH variable before running maven.

AlexFalappa avatar Jan 07 '20 14:01 AlexFalappa

Possibly same problem as in #46.

AlexFalappa avatar Jan 08 '20 10:01 AlexFalappa

@AlexFalappa I'm still getting this issue even with javafx-maven-plugin version 0.0.4.

Setting configuration.executable works, but its not an acceptable solution as it is hardcoded to my build environment.

daniel-shuy avatar Mar 10 '20 17:03 daniel-shuy

Turns out my scenario is the opposite, my %JAVA_HOME% is pointing to JDK 8 (as there are some Java applications that still require Java 8 to run).

I don't understand why doesn't the javafx-maven-plugin use the JDK version used by Maven.

daniel-shuy avatar Mar 10 '20 18:03 daniel-shuy

@daniel-shuy isn't Maven using the JDK pointed to by JAVA_HOME?

AlexFalappa avatar Mar 11 '20 15:03 AlexFalappa

@daniel-shuy isn't Maven using the JDK pointed to by ?JAVA_HOME

Is not always that way. If, for example, the project is running from an IDE (in my case IntelliJ) Maven ignores JAVA_HOME and instead uses the JDK defined in the IDE project settings.

@daniel-shuy @AlexFalappa in any case, this plugin always uses the JDK that Maven is running on.

In order to know which executable is being used in your case, run the command with -X option

$ mvn clean compile javafx:run -X

and find the line [DEBUG] Executable [D:\tools\java\adoptopenjdk-13+33_hotspot\bin\java.exe]

Untitled

betanzos avatar Mar 11 '20 21:03 betanzos

For what it's worth, I ended up just using a JDK/JRE that has JavaFX bundled with it (I went with Liberica).

HenryDeLange avatar Mar 12 '20 12:03 HenryDeLange

In any case it would be interesting to be able to determine the cause of the problem to fix it if it were necessary.

betanzos avatar Mar 12 '20 16:03 betanzos

in any case, this plugin always uses the JDK that Maven is running on.

@betanzos How is that the case?

https://github.com/openjfx/javafx-maven-plugin/blob/84cf5738f58010182222b700d0e5d77a0e1a2a79/src/main/java/org/openjfx/JavaFXBaseMojo.java#L345

If no explicit override is defined, it uses JAVA_HOME, which you yourself said doesn't need to refer to the JDK that Maven is running with if it's started via an IDE.

I came here because I just helped someone fix an issue where Maven was running with Java 11 (chosen in the IDE) and this plugin used the JVM that was configured in JAVA_HOME.

[INFO] --- javafx-maven-plugin:0.0.4:run (default-cli) @ Redacted ---
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Unrecognized option: --module-path
[ERROR] Command execution failed.
org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInternal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.openjfx.JavaFXBaseMojo.executeCommandLine (JavaFXBaseMojo.java:504)
    at org.openjfx.JavaFXBaseMojo.executeCommandLine (JavaFXBaseMojo.java:394)
    at org.openjfx.JavaFXRunMojo.execute (JavaFXRunMojo.java:100)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)

Changing JAVA_HOME to JDK 11 and restarting IntelliJ fixed the issue.

alshain avatar Apr 11 '20 11:04 alshain

If no explicit override is defined, it uses JAVA_HOME, which you yourself said doesn't need to refer to the JDK that Maven is running with if it's started via an IDE.

@alshain you are right.

@abhinayagarwal @jperedadnr what should be the correct behavior in this case?

My opinion is that if the executable (or jlinkExecutable for javafx:jlink) to be used is not explicitly specified, the same one that Maven runs on should be used instead JAVA_HOME.

betanzos avatar Apr 11 '20 17:04 betanzos

The issue I had with this is that maven project overrides did not supersede JAVA_HOME as the should. JAVA_HOME is supposed to be a fallback not the first tried variable. The convention in Maven 3 is to use toolchains.xml and the toolchain plugin to configure the compiler and target runtime. This convention seems to be ignored by this plugin. I rely on JAVA_HOME for things like IDEs to use a stable supported JDK so I have this set to a Java8 JDK. The only way I could get the javafx-maven-plugin to use the correct JDK was to unset JAVA_HOME.

dwhitla avatar May 18 '20 03:05 dwhitla

Just following the Getting Started guide - from the perspective of someone with a lot of Maven experience who therefore defaults to using Maven to build things - results in one showstopper after another. As a side note that is not great for developer adoption.

After moving on from JAVA_HOME I found that JavaFXJLinkMojo.jlinkExecutable is not resolved against the current JRE. This results in errors like the following unless you hard code the path to jlink in your POM.

Caused by: java.io.IOException: Cannot run program "jlink" (in directory "/Users/dwhitla/dev/code/samples/java/HelloJFX"): error=2, No such file or directory

Specifically JavaFXBaseMojo.getExecutablePath() does not perform as expected (the comment explains why) - the plugin jumps straight to looking for $JAVA_HOME instead of consulting the toolchain properties or the currently executing JRE ...

image

dwhitla avatar May 18 '20 04:05 dwhitla

I should probably explain why this is such a big deal. I, and most of my team, develop on MacOS. Our CI environment is Linux, and our target environments for JavaFX are Windows, MacOS and various Linux variants - that is kinda the point of us using JavaFX. I can't hard code system paths in POMs. That is what toolchains.xml is for. This is not a blocker as we can use the process environment now I understand what is happening inside the plugin. But as mentioned above that causes other issues for development environments.

dwhitla avatar May 18 '20 04:05 dwhitla

I have fixed this issue in https://github.com/openjfx/javafx-maven-plugin/pull/87 but have closed that PR while I refactor the test I had to disable due to https://github.com/openjfx/javafx-maven-plugin/issues/86. I will fix that test today and create a new PR.

The behaviour I have implemented tries to resolve the executable and jlinkExecutable properties by:

  • treating absolute paths as a hard override; then
  • testing unqualified paths against:
    • the configured maven JDK toolchain when defined; else
    • the JDK parent of the invoking JRE when it exists; else
    • the collection of paths enumerated from the system PATH environment variable when defined; then
  • testing any remaining unmatched path as relative to:
    • the workingDirectory when defined in the plugin configuration; else
    • the project basedir

If the supplied executable cannot be matched by any of the above a MojoExecutionException is thrown indicating that the supplied executable path could not be resolved.

I specifically removed the current directory from existing search logic because implicitly (and silently) adding . to your PATH is a security hole.

image

dwhitla avatar May 18 '20 21:05 dwhitla

Just now, I also encountered this problem:

Caused by: java.io.IOException: Cannot run program "jlink" (in directory "/【...】"): error=2, No such file or directory

Running jlink on Windows is normal, but Linux fails.

I tried several times and succeeded after adding additional environment parameter 【JAVA_HOME】.

Project related information:

  • JDK17.0.5
  • Spring Tool Suite 4.17.0
  • Maven 3.8.6

wang3develop avatar Dec 30 '22 04:12 wang3develop

Using the ${java.home} variable available inside Maven fixed this issue for me. Plugin was ignoring the IDE's selected JDK (IntelliJ)

<configuration>
   ...
    <executable>${java.home}/bin/java</executable>
   ...
</configuration>

Maven 3.9.2 OpenJDK 20.2

magno32 avatar Aug 28 '23 14:08 magno32