9

I have created a module com.company.ep that is located in the source folder com.company.ep. (Yes, I have removed src from the build path and deleted it!) Inside the source folder, I have a couple of packages as the following:

com.company.ep    <--- root source folder
    com.company.ep.main    <--- package 1
    com.company.ep.model   <--- package 2
    com.company.ep.view    <--- package 3
    // ... more packages
    module-info.java

The main class is located in the package com.company.ep.main.Main. In my module-info.java, I have configured the dependencies:

module com.company.ep {
    exports com.company.ep.main;
    exports com.company.ep.model;
    exports com.company.ep.view;
    // ... more exports
    requires javafx.controls;
    requires javafx.graphics;
}

When I tried to launch my program, eclipse told me that:

Error occurred during initialization of boot layer
java.lang.module.FindException: Module javafx.controls not found, required by com.company.ep

So, I tried to run it on the command prompt:

java -p d:\Applications\openjfx-sdk-11\lib;bin -m com.company.ep/com.company.ep.main.Main

bin is the output folder of eclipse, and it worked.

So, I went to Properties → Run/Debug Settings → Main → Show Command Line, it showed:

D:\Applications\openjdk-11.0.1\bin\javaw.exe -Dfile.encoding=UTF-8 -p "D:\Development\Eclipse-Workspace\MyProject\bin" -classpath "D:\Applications\openjfx-sdk-11\lib\javafx.base.jar;D:\Applications\openjfx-sdk-11\lib\javafx.controls.jar;D:\Applications\openjfx-sdk-11\lib\javafx.fxml.jar;D:\Applications\openjfx-sdk-11\lib\javafx.graphics.jar;D:\Applications\openjfx-sdk-11\lib\javafx.media.jar;D:\Applications\openjfx-sdk-11\lib\javafx.swing.jar;D:\Applications\openjfx-sdk-11\lib\javafx.web.jar;D:\Applications\openjfx-sdk-11\lib\javafx-swt.jar" -m com.company.ep/com.company.ep.main.Main

I have created a user library with all JARs added, and the library is added to the project's Modulepath.

Then I have tried to set the module path explicitly in VM arguments in Run/Debug Settings: -p D:\Applications\openjfx-sdk-11\lib, I'd still no luck.

My questions are:

  • Why javaw.exe?
  • Why classpath? As my library is added as a module-path entry.
  • How to configure the module dependencies in eclipse.

I am not sure if I have configured eclipse correctly, or whether it is probably a problem of OpenJDK as it worked when I worked on another computer with Oracle Java SE installed.

Thank you!

Sae1962
  • 1,122
  • 15
  • 31
vesontio
  • 381
  • 1
  • 7
  • 19
  • 1
    Are you using the latest version of Eclipse? Previous versions had lots of problems handling the module path correctly. – mipa Nov 14 '18 at 08:52
  • @kleopatra The OP said that he has a module-info.java in which he requires the JavaFX modules. In this case it should not be necessary to add --add-modules commands. With the latest version of Ecipse the handling of the module path in the runtime configuration should also work without having to specify any --module-path explicitly. – mipa Nov 14 '18 at 08:58
  • @kleopatra I have tried `--add-modules`, but it still doesn't work. By the way, I still don't understand this directive, as all the necessary modules are supposed be located in my module-path. In addition I can launch my program on command line with `-p` and `-m`. – vesontio Nov 14 '18 at 08:59
  • @mipa Yeah, I am using Eclipse 4.9.0, which is the latest version, I think, and I have also installed the Java 11 support from market place. I am using OpenJDK ver11 since I need the OpenJFX 11 SDK. As you said, theoretically, Eclipse is supposed to be able to handle modulepath automatically, as it has added my `bin` directory into module path. – vesontio Nov 14 '18 at 09:03
  • @vesontio In this case everything should be ok and work without any additional command line options. So there must be another problem. What I find suspicious is your folder structure. Why did you not stick with the usual folder conventions? – mipa Nov 14 '18 at 09:14
  • @mipa - just re-checked my local setup, there had been some leftover from previous versions: you are right, they are not needed any longer, thanks for the heads up :) – kleopatra Nov 14 '18 at 09:17
  • the latest update is 2018-9, are you sure that's what you have? just asking because having the modules on the classpath looks like a bug in earlier versions that has been fixed – kleopatra Nov 14 '18 at 09:19
  • @kleopatra Yes, that is the version that I have :) – vesontio Nov 14 '18 at 14:58
  • @mipa In fact, I was just messing with my folder structures, I had placed all my packages inside `src`, but when I read the project jigsaw introduction on java.net, they said *By convention, the source code for the module is in a directory that is the name of the module*. – vesontio Nov 14 '18 at 15:01

1 Answers1

11

The explanation of why Eclipse fails on running your modular project can be found in the OpenJFX docs for Eclipse (modular from IDE section).

As it was already mentioned:

Being a modular project, and since we already added the JavaFX SDK library to the module-path, there is no need to add any VM arguments.

But if you run on Eclipse you will get the mentioned error:

Error occurred during initialization of boot layer java.lang.module.FindException: Module javafx.graphics not found, required by hellofx

So why is it failing??

As explained in the docs:

This exception happens because the Eclipse ant task overrides the module-path

How does this happen??

Checking the command line applied (Show Command Line from Run Configurations...), you can find out why:

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p bin/hellofx \
    -classpath $PATH_TO_FX \
    -m hellofx/org.openjfx.MainApp 

If you copy it and paste it and run it in a terminal, it will fail of course with the same message. The reason is that Eclipse doesn't add the JavaFX library to the module path.

If the task generates the wrong arguments, let's try to fix it by adding our own VM arguments by editing Run configurations... and adding -p $PATH_TO_FX:bin/hellofx.

But if you run it, it will fail again.

Let's check why, with Show Command Line from Run Configurations...

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p $PATH_TO_FX:bin/hellofx \
    -p bin/hellofx \
    -classpath $PATH_TO_FX \
    -m hellofx/org.openjfx.MainApp 

As you can see, the user's VM arguments are added before the default ant task arguments, so there are two -p (--module-path) options, and the first one (the user's one with the JavaFX jars) is overridden by the second one (only the project's module), so, again, the JavaFX jars are not added to the module path, and hence you get the error.

So how can we fix it??

As mentioned in the linked documentation, the possible fix is:

To prevent this issue click on Run -> Run Configurations... -> Java Application -> Dependencies, select Override Dependencies... and add -p /path-to/javafx-sdk-11/lib:bin/hellofx, and press Override.

With this solution, you can see it works, and you can check the command line:

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p $PATH_TO_FX:bin/hellofx \
    -p bin/hellofx \
    -classpath $PATH_TO_FX \
    -p /path-to/javafx-sdk-11/lib:bin/hellofx \
    -m hellofx/org.openjfx.MainApp 

Basically we are adding again the "right" module path option, after all the failed ones.

While now the project runs, the solution is obviously not nice.

Here you can find a sample referred from the OpenJFX documentation.

EDIT

Based on @kleopatra comments, another workaround to make it work is the following:

For some reason, the library JavaFX11 (that contains modular jars) is not scanned and Eclipse doesn't include those jars into its -p option, but into the classpath:

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p bin/hellofx \
    -classpath $PATH_TO_FX \
    ...

But, if you add those jars directly to the module path, it will do add them, and this will run fine:

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p bin/hellofx:$PATH_TO_FX/javafx.base.jar:...:$PATH_TO_FX/javafx.controls \
    ...

eclipse module-path

Then with this there is no more need to override the dependencies.

EDIT 2

As @mipa points out in a comment, there was a bug filed on this issue, and it has already been solved. I've tested it with Eclipse 2018-12 M2 (4.10.0M2) Build id: 20181108-1653, and it works with the JavaFX11 library only (as it should):

$JAVA_HOME/bin/java -Dfile.encoding=UTF-8 \
    -p bin/hellofx:$PATH_TO_FX/javafx.base.jar:... \
    -m hellofx/org.openjfx.MainApp 

Eclipse 4.10

José Pereda
  • 44,311
  • 7
  • 104
  • 132
  • hmm ... not entirely convinced that an override of the dependencies (per runtime config of the individual app) is needed: since 2018-9 I don't see any of the previous "unknown/missing" module errors, modular projects just run fine without additional runtime arguments anywhere (except the normal add-exports when accessing hidden api). The difference in setup (compared to your description) might be that I do _not_ add the modules via a userlib - there was some weird behaviour when trying - but as single modules. – kleopatra Nov 14 '18 at 12:58
  • though, can't say if that might make a difference: never seen the override of a custom --add-modules/--module-path as global runtime vm arg not working, so there might be some other difference as well – kleopatra Nov 14 '18 at 12:59
  • @kleopatra Maybe you can check if this [sample](https://github.com/openjfx/samples/tree/master/IDE/Eclipse/Modular/Java) works for you with or without this workaround? – José Pereda Nov 14 '18 at 13:19
  • 1
    did: import the IDE eclipse modular project, configured a userLib named JavaFX11 to point to my fx11 jars, removed the launch file (it uses some environment vars that I don't have plus giving something like illegal char, didn't dig), pointed the jre to my current openjdk11 - running gives "cannot find module javafx.base". Replaced the userlib with adding the individual jars -> all fine – kleopatra Nov 14 '18 at 13:45
  • 1
    Right, it works. So basically it is _another_ workaround, but more "clean". When you add the jars instead of the library folder, Eclipse finds those jars are actually modules and add them to the path... I'll edit my answer (and the docs with this approach as it looks better) – José Pereda Nov 14 '18 at 14:11
  • 3
    actually I think it's a bug in Eclipse: it should be able to handle the modules correctly if they are provided in a userlib ... as is, it's another chapter in love-hate story between Eclipse and modules ;) – kleopatra Nov 14 '18 at 14:20
  • 1
    Yes, I think so too. The OpenJFX docs about Eclipse actually mention possible bugs (like when on Gradle, you add a `module-info` file and then JavaFX classes are not recognized anymore unless you add the SDK). Feel free to provide some feedback. – José Pereda Nov 14 '18 at 14:30
  • 1
    Some time ago I also fiddled around with userlibs but finally gave up due to strange errors. After that I always added the jars explicitly and since Eclipse 4.9 this finally works. I think it is important that people have an easy and reliable way of working with JavaFX and are not frightened away by horrible workarounds for tooling problems which are actually not JavaFX's fault. – mipa Nov 14 '18 at 19:44
  • @mipa I agree obviously on the need of finding the way "to have an easy and reliable way of working with JavaFX", without using (I wouldn't use horrible though) workarounds. But as long as there are issues with IDEs or build tools, we'll need to find how to do the job anyway. As mentioned before, anyone is welcome to provide feedback to improve the JavaFX [docs](https://openjfx.io/openjfx-docs/). – José Pereda Nov 14 '18 at 23:12
  • 1
    It seems that the Eclipse issue, that the content of the user library is not added to the module path, has already been fixed in the latest pre-release of Eclipse (4.10 M3). See https://bugs.eclipse.org/bugs/show_bug.cgi?id=540305 – mipa Nov 15 '18 at 10:48
  • I've edited my answer, with the link to a new Eclipse version that has fixed this issue – José Pereda Nov 15 '18 at 12:27