JavaFX project cannot find external libs using Java 11 - java

I'm using OpenJDK 11, IntelijIDEA 2019.2 and javafx-sdk-11.0.2.
When I wrote JavaFX project, I tried to add external runnable jar from maven project, but IntelijIDEA didn't see classes for this jar.
What I've done:
I added as external library own jar.
In the project tree I found it:
But I couldn't create class objects and use methods that contains this jar:
Why it happens?

If it's modular project
The reason was the use of the file module-info.java. As we know, JavaFX 11 is not part of the JDK anymore. So, we need to add this special file at the root of our packages w/ lines like:
module modulename {
requires javafx.fxml;
requires javafx.controls;
opens package;
}
From this moment, most likely you won't find classes until you add separately your jar in this code like:
requires name_of_jar;
Only after adding this, you can use your classes/methods from external libs.
If it's non-modular project
As mentioned by mipa, you can follow these instructions as alternative way.
Related links:
https://www.baeldung.com/java-9-modularity
https://medium.com/criciumadev/its-time-migrating-to-java-11-5eb3868354f9
How to use 3rd party library in Java9 module?
https://www.oracle.com/corporate/features/understanding-java-9-modules.html
IntelliJ can't recognize JavaFX 11 with OpenJDK 11

Related

Will creating non modular application require me to separate my modular from non-modular dependencies

I am upgrading a legacy application to java 11. Currently we don't use maven or gradle.
As i've updated it, i've replaced certain libraries that have been removed from the jdk in 11. Some of the newer dependencies i've added contain module-info.java files. When I go to create a custom jre for my application using jdeps (in preparation for jlink), i have the option of specifying a class path and a module path. My question is, can the paths be the same 'lib/*' directory? In my mind this would try to use the jars as both modular jars and regular jars. If i must separate them, maybe there is a tool to help me know which ones need to be put in a separate directory (identify jar's containing 'module-info.java') and give me a list of them.
In short, yes you do need to put modular jars in a separate directory than your non modular jar dependencies for jdeps.
Here is a great video on the module system:
https://www.youtube.com/watch?v=M7q3C8OwJe8

Java build issues - Java 8 to Java 11 migration

We're migrating from Java 8 to Java 11.
We have a legacy project Y which depends on another legacy project X.
The project X has no sources, it's just a collection of about 300 jars.
The build is ant-based, no maven.
I cannot build the project Y now with JDK 11 (neither in Eclipse, nor externally)
because it says "The package org.w3c.dom is accessible from more than one module: , java.xml"
I get this error in Eclipse on a line which does import org.w3c.dom.Document;
When I do an external build (with ant, outside of Eclipse) I can build successfully (with basically the same build.xml as under JDK 8)?! How come only Eclipse is complaining?! Is it because of this javac bug which I reference below.
I was reading here (these are directly related to my issues):
https://stackoverflow.com/a/53824670/2300597
The package org.w3c.dom is accessible from more than one module: <unnamed>, java.xml
http://mail.openjdk.java.net/pipermail/jigsaw-dev/2018-December/014077.html
but I am still unable to fix these build issues.
I tried 7-8 different things but nothing helps.
I think the clash is between org.w3c.dom package from some of these 300 jars and the same package in the JDK module java.xml.
The weird thing is that org.w3c.dom.Document doesn't seem to be present in any of these 300 jars, it's just that other classes from the same package are present in jars.
I am deadlocked, I see no way to fix this.
I cannot lightly change project X because it's a shared library used by multiple legacy projects.
On the other hand, I cannot remove java.xml module from the build path of project Y.
So I don't know how to approach this.
Isn't there some way to just say: OK, use the classes from the JDK first, then use those from the JARs (even though they share the same package, they are not the same class).
What is the right way to fix these errors?
Starting from version 9 Java supports JPMS (Java Platform Module System).
In the Java Platform Module System it is not allowed to use the same package in more than one module. So, the solution is to find modules (jars) that exports this package org.w3c.dom and to remove one of the modules or package in it.

Library creation in JAVA using modules in JAVA 9

I want to create a library in JAVA with all its dependencies contained in it so that the versions of the dependencies do not conflict with the versions that may be available in the environment in which the library will be used. I have explored the concept of a module in JAVA. I was also able to create a module using Maven in Eclipse. Now I am not sure how to create a JAR out of it. Or is there any alternative to a modular jar for such a library. ( P.S. I am very new to JAVA)
If I understand you correctly it is not a module you would like to do, but rather a distribution with all jar files included.
To fix this you could do a fat jar with Maven.

Migrate to java11

I have a big gradle project with a lot of gradle modules.
I want to add java11 support with back compatibility (with java 8).
1) Do I have to use java9 modules system, or such migration is possible without it ?
2) If yes, can I auto-generate module-info files automatically, my project is huge.
If not defined otherwise all you classes will be packed into a unnamed module.
But you should be able to run your app without code modifications.

javafx.base reads package javafx.beans

I'm using OpenJDK 11 and OpenJFX 11.0.1 with Gradle in IntelliJ IDEA for my project.
When I use javafx.base module, IntelliJ IDEA reports an error at line 1 (project module declaration) saying I'm not including the javafx.beans module (beans is a package in javafx.base module) in my module requirements.
Gradle jar task executes without any errors or warnings so this error is likely related to IntelliJ IDEA.
Error message: "Module 'hr.caellian.lunar' reads package 'javafx.beans' for both 'javafx.base' and 'javafx.base'"
If I remove 'javafx.base' requirement, 'javafx.base' from above error is replaced with the next javafx module.
I'm also getting a warnings at javafx module requirement lines: "Ambiguous module reference: javafx.base" for 'javafx.base' and so on for each javafx module.
Here's my module-info.java file:
module hr.caellian.lunar {
requires java.base;
requires kotlin.stdlib;
requires javafx.base;
requires javafx.controls;
requires javafx.fxml;
opens hr.caellian.lunar.gui to javafx.graphics;
requires org.kordamp.ikonli.javafx;
requires org.kordamp.iconli.core;
requires com.fasterxml.jackson.core;
requires com.fasterxml.jackson.databind;
requires com.fasterxml.jackson.dataformat.yaml;
requires arglib;
requires org.apache.bcel;
}
I looked online for a solution for at least half an hour now. All I found was a website saying only the modules I already included are necessary for JFX (which was for JavaFX 9 I think).
I redownloaded JavaFX modules to see if I missed copying something but I didn't - javafx.beans module doesn't exist.
'java --list-modules' doesn't list any javafx modules if that's a problem, although I gathered that it should only list jdk modules and OpenJFX is no longer included with OpenJDK.
I installed OpenJFX SDK into JDK folder (lib files copied to lib folder, same for legal), added javafx jar dependency files from lib folder of JDK11 in IDEA manually. I also added all jmod files to jmod folder of JDK11.
Is it possible JavaFX modules have bad module-info.java files or something like that? Is this IntelliJ IDEA bug?
Managed to fix the problem thanks to the comment from #nullpointer.
The problem was that I had copied files from JavaFX SDK to both my JDK 11 classpath and project lib folder.
If you're seeing this error with the same warnings that means you probably have duplicate module jars (maybe in future modules directly) in your IntelliJ project JDK Classpath and in your build.gradle (or lib folder).
How to fix:
I removed OpenJFX SDK jars from JDK11 Classpath in IntelliJ IDEA in Project Structure. As I deleted JFX SDK jars from my project lib folder, I readded them. Gradle adds every jar in lib folder with:
implementation fileTree(dir: 'lib', include: '**/*.jar')
Also, do not install JavaFX directly into JDK folder. It'll be impossible to update either without keeping unnecessary files, you'll never be able to find all JFX files to remove in case you want yo remove it, JFX src.zip replaces JDK src.zip in lib folder. They're meant to be separate.

Categories