Just out of curiosity, I'm trying to setup Eclipse to allow it to compile and run an application with a standard library class being patched.
I have 2 Java projects patch
and consumer
: one containing a string patch class (with a new method size()
, identical to length()
) and the other which should use said method size()
. The setup is the following one:
- patch
- bin (contains class files)
- src
- java
- lang
- String.java
- consumer
- src
- consumer
- Main.java
- module-info.java
Main.java:
package consumer;
public class Main {
public static void main(String[] args) {
String s = new String("hello");
System.out.println(s.size());
}
}
After compiling patch
(thereby getting String.class
inside patch/bin/java/lang/
) I know I can easily use:
java --patch-module java.base=patchpjt/bin/ consumer/src/consumer/Main.java
To correctly call the newly added method size()
, obtainining the result of 5
.
The problem is that in Eclipse method size
is still not recognized (the error is Method Size() is underfined for type String):
So my question is:
consumer
to correctly compile, run and show no errors?size()
?I know I need to use the Build Path -> Module Path -> Edit Is Modular -> Details Tab -> Patched module
but I don't know how to configure both projects.
Some info you might find useful:
Thanks for any kind reply.
You are using --patch-module
for which it is not intended (JEP 261: Module System):
The --patch-module option is intended only for testing and debugging. Its use in production settings is strongly discouraged.
In your scenario, you do not patch an existing method but add an additional method which breaks the API of the system library. Eclipse supports only patching without breaking the API of the system library. The fact that javac
does not show any error (probably due to missing checks if the API will not be broken) is a bug in my opinion.
If you create your own JRE, add it in Window > Preferences: Java > Installed JREs and make sure when creating a new Java project to not select an execution environment JRE, but your specific JRE.