4

Using Maven and JavaFX in Intellij (2019.1). I have been following this tutorial.

I have a curious error that keeps occurring - every time I keep running the javafx:run plugin, it fails, giving this error:

Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
Unrecognized option: --module-path

However, when I put the executable in the javafx-maven-plugin (<executable>"C:\Program Files\Java\jdk-12.0.1\bin\java.exe"</executable>) it works. I am on Windows and have set the JAVA_HOME system environment variable to C:\Program Files\Java\jdk-12.0.1 which is where the JDK is installed.

This is a curious issue that is not critical, but would be nice to know the answer to.

EDIT:

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>uk.co.harveyellis</groupId>
    <artifactId>HelloFX</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>12</maven.compiler.source>
        <maven.compiler.target>12</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-controls</artifactId>
            <version>11.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.openjfx</groupId>
            <artifactId>javafx-fxml</artifactId>
            <version>11.0.2</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <release>12</release>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.openjfx</groupId>
                <artifactId>javafx-maven-plugin</artifactId>
                <version>0.0.2</version>
                <configuration>
                    <mainClass>uk.co.harveyellis.App</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Other pictures:

Project language level Project outline

Environment Path

C:\Rtools\bin
C:\Program Files\Microsoft MPI\Bin\
C:\Python37\Scripts\
C:\Python37\
C:\WINDOWS\system32
C:\WINDOWS
C:\WINDOWS\System32\Wbem
C:\WINDOWS\System32\WindowsPowerShell\v1.0\
C:\WINDOWS\System32\OpenSSH\
C:\Program Files\Java\jdk-12.0.1\bin
C:\Program Files\apache-maven-3.6.1\bin
C:\Program Files (x86)\Common Files\Oracle\Java\javapath
C:\ProgramData\chocolatey\bin
C:\Program Files (x86)\Brackets\command
C:\Program Files\Git\cmd
C:\Program Files\dotnet\
C:\Program Files\Microsoft SQL Server\130\Tools\Binn\
C:\Program Files\PuTTY\
C:\Program Files\nodejs\
C:\Program Files\Gradle\gradle-5.4\bin

Note also that C:\Program Files\JetBrains\IntelliJ IDEA 2019.1.1\bin is in user path.

Omid
  • 5,823
  • 4
  • 41
  • 50
Harvey Ellis
  • 596
  • 1
  • 3
  • 12
  • Can you try the JavaFX plugin version 0.0.2? – José Pereda Apr 25 '19 at 18:19
  • @JoséPereda Ok, will give that a go! – Harvey Ellis Apr 25 '19 at 18:20
  • @JoséPereda - still the same problem I'm afraid. It still does work with the executable explicitly specified though. – Harvey Ellis Apr 25 '19 at 18:27
  • Have you set the project SDK and language level to Level 12? – José Pereda Apr 25 '19 at 18:31
  • @JoséPereda I think so - and it is set to that in the intellij settings too – Harvey Ellis Apr 25 '19 at 18:39
  • 1
    Can you print what you have in the `PATH` variable (`echo %PATH%`) in a terminal? Probably you will have the path to a JDK 8 included, before a JDK 12 if present. – José Pereda Apr 25 '19 at 18:57
  • @JoséPereda I will check that now, I'd imagine the Oracle JRE 8 is in there somewhere! Good shout! – Harvey Ellis Apr 25 '19 at 19:02
  • @JoséPereda I've tried removing the Oracle JRE entry but it still didn't work. Is there some order in which the plugin looks for Java.exe? Does it look in system or user environment first? In addition - when I run `java` from the command line the version that runs is 12.0.1 – Harvey Ellis Apr 25 '19 at 19:25
  • You can see how it is retrieved from the plugin's [source code](https://github.com/openjfx/javafx-maven-plugin/blob/master/src/main/java/org/openjfx/JavaFXBaseMojo.java#L308). – José Pereda Apr 25 '19 at 19:32
  • @JoséPereda Thanks, I'll have a look. – Harvey Ellis Apr 25 '19 at 21:19
  • Let me know how I can help you solve this, or if you want, file an [issue](https://github.com/openjfx/javafx-maven-plugin/issues) and we can discuss it there. – José Pereda Apr 26 '19 at 10:09
  • 1
    If you edit the run configuration on IntelliJ, and add this command line `clean -X javafx:run`, when you run it you should see printed the executable's actual path of the JDK used (probably 1.8). – José Pereda Apr 26 '19 at 11:32
  • @JoséPereda Thanks again. I have had a look at the version that is being used, and it is Java 12.0.1. I have tried compiling using `javafx:compile` and then run and it works. I have compared the outputs of the two compiles using winmerge, and there seems to be a big difference after two lines which say: `[INFO] --- javafx-maven-plugin:0.0.1:compile (default-cli) @ HelloFX --- [DEBUG] Dependency collection stats:` Lots of dependencies get loaded in the Javafx:compile, but nothing seems to happen in the normal maven compile section. – Harvey Ellis May 01 '19 at 16:34

3 Answers3

6

For future viewers, the answer turned out to be very simple: the instructions for maven at the getting started with JavaFX are with intellij and maven (non-module version), as found here are slightly incorrect.

The instructions are as follows:

You can open the Maven Projects window and click on HelloFX -> Plugins -> compiler -> compiler:compile to compile the project, and click on HelloFX -> Plugins -> javafx -> javafx:run to execute the project.

The key part that is wrong here is that if you are using a project that uses static resources - like the FXML files in the HelloFX project - then compiling only using compiler:compile will not copy these files into the target\classes directory.

This is a subtle mistake in the guide - presumably because if you build from command line nothing will be wrong - using mvn clean javafx:run will perform all the steps in between. Therefore, the instructions need to be to run compiler:compile and resources:resources for the thing to work in Intellij.

Alternatively, the guide could be changed to say just run javafx:compile or run the lifecycle phase called package in intellij, and then run javafx:run.

Harvey Ellis
  • 596
  • 1
  • 3
  • 12
5

I've been experiencing similar pains with setting up a JavaFX project with Maven. That command requires a newer version of the JDK and the issue at hand is that Maven will actually ignore your system's JAVA_HOME and JDK_HOME variables, as well as your IDE settings.

You can check which version of Java that Maven is using by simply executing this command in your console:

$ java -version

In the case of Windows, you just move the entry pointing to the newer Java version above the older one, like so:

enter image description here

J. Willus
  • 537
  • 1
  • 5
  • 15
  • This was it for me, too. Incidentally I had both a user variable "Path" and a system variable "Path", and maven ignored the user variable. I had to modify the global Path to get it working. – workerjoe Feb 27 '20 at 18:21
0

To see if you have JAVA_HOME set open cmd prompt:

echo %JAVA_HOME%

If nothing prints on the console, you need to set that variable, even if is already on the PATH variable, those are two different environment variables.

thefern
  • 367
  • 7
  • 19