Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to run JavaFX via Maven #50

Open
HenryDeLange opened this issue Nov 27, 2019 · 17 comments
Open

Unable to run JavaFX via Maven #50

HenryDeLange opened this issue Nov 27, 2019 · 17 comments

Comments

@HenryDeLange
Copy link

HenryDeLange commented Nov 27, 2019

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.

@tirnavali
Copy link

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> 

@AlexFalappa
Copy link
Contributor

AlexFalappa commented Jan 7, 2020

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
Copy link
Contributor

Possibly same problem as in #46.

@daniel-shuy
Copy link

@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
Copy link

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.

@AlexFalappa
Copy link
Contributor

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

@betanzos
Copy link
Contributor

@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

@HenryDeLange
Copy link
Author

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

@betanzos
Copy link
Contributor

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

@alshain
Copy link

alshain commented Apr 11, 2020

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

@betanzos How is that the case?

CommandLine getExecutablePath(String executable, Map<String, String> enviro, File dir) {

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.

@betanzos
Copy link
Contributor

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.

@dwhitla
Copy link

dwhitla commented May 18, 2020

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
Copy link

dwhitla commented May 18, 2020

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
Copy link

dwhitla commented May 18, 2020

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
Copy link

dwhitla commented May 18, 2020

I have fixed this issue in #87 but have closed that PR while I refactor the test I had to disable due to #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

@wang3develop
Copy link

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

@magno32
Copy link

magno32 commented Aug 28, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants