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.
Related
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
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.
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
I have a Java project which is heavily used by all sorts of other Java and Android projects. The project contains some JAR libraries which shall be used by all projects, except for the Android one (in fact the Android project is a Android library project to be precise).
I marked the JARs as "export" in the Eclipse build path preferences of the Java project. However, the Android project shouldn't import these libraries (as they are Java libraries which make use of some classes which are not available on Android), but it shall import the rest of the code (which doesn't really use the libraries, but they are stored in there for convenience reasons and to ensure, that all other projects use the same library.
How can I prevent the JARs from being exported to the Android projects?
You can prevent all jars from being exported so that only the common project is a dependency for each project that needs it.
Then you can change the build path of each project to only include its necessary jars through the add jar.. dialog in build properties.
That's the easiest way.
A more extreme way would be to move to maven and then eclipse will only include the jars you specify in the pom - though that's a load of extra work for not much gain.
Alternatively, you could split the android specific code into a android-common separate project and then make your common project depend on it and export it - then your android project could rely on this android-common project instead of the existing common project.
There is a class, that compiles with the android platform and the jdk. Now, the idea is, to use that class in two projects, one is a android one and the other result in a java application.
So, I cretae a project with two modules, one for the android and one for the java solution. All works fine, with copy&paste the class-source between the two modules, but that is not very comfortable to use.
So, the question is, how it can be done without copy&paste. I would prefer a intellij solution, but I am willing to change to eclipse if needed.
Thanks for all answers.
FWIW, here's what I have working using NetBeans 7.1.1 ... it's possible that some of these steps aren't needed, but after a couple of days experimenting this seems to work.
Create a "Shared1" Java Library containing the shared classes & their source code
Add the "Shared1" library to the desktop Java project
Create a "Shared" Android project... in Properties, tick "Is Library" and set up a Custom location "../Shared1.dist"
Add the "Shared" project to the Libraries in the Android application project
Add "Shared1" as a JAR library (Custom location "../Shared1.dist") in the Android application project
The classes in the "Shared1" project are now usable in both the desktop environment and the Android environment.
BEWARE!!!! Using java.awt classes in the "Shared" library classes doesn't generate any compiler warnings, but causes the Android application to crash. Subsequent runs also crash, even if the java.awt reference has been removed and Clean & Build done on everything!!!
Create a MyLibrary project containing the shared class, package it as a jar (MyLibrary.jar), and add this jar as a library of the Java and Android projects.
Since you say the class is "common", that implies it doesn't use any Android-specific classes. In this case, you can just distribute the common code as a JAR library to the java and Android projects.
Unfortunately I do not really understand what does it mean project with 2 modules.
Typical solution for this problem is 3 projects:
android project
other project
shared (utility) project
Projects 1 and 2 depend on project 3.
If you are using maven it is pretty simple to do: you just have to add appropriate dependency tag. For example if your utility project's artifact is "com.mycompany.util" add definition like the following to pom.xml of your dependent projects:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>util</artifactId>
<version>1.0</version>
</dependency>