How can I use a custom classpath when overriding a native class like java.util.regex.Matcher
in a non-modular code base.
Basically what I want to do is to use a different class path like --class-path build/custom
when patching a class. Because the modified Matcher.java have references to classes that are compiled into build/custom
.
I.E:
Say I have modified java.util.regex.Matcher
and now it requires some other (non-java) external.jar to function.
So how can I patch it like:
javac --patch-module java.base=src
, so it also finds the classes in the external.jar.
Original case
Directory structure:
+- build
+- src
+- PATCH
+- java
+- util
+- regex
+- Matcher.java
+- CUSTOM
+- com
+- example
+- MyClass.java
Two classes are as follows:
java.util.regex.Matcher:
package java.util.regex;
class Matcher {
com.example.MyClass myClass;
}
com.example.MyClass:
package com.example;
public class MyClass {
}
CASE #1
This works:
javac -d build src/CUSTOM/com/example/MyClass.java
javac -d build --patch-module java.base=src src/PATCH/java/util/regex/Matcher.java
CASE #2
But if I compiled MyClass into a custom build path like (-d build/custom
)
javac -d build/custom src/CUSTOM/com/example/MyClass.java
javac -d build --class-path build/custom --patch-module java.base=src src/PATCH/java/util/regex/Matcher.java
It results in an error:
src/Matcher.java:4: error: package com.example does not exist
com.example.MyClass myClass;
^
1 error
Test Repo
I created a test repo: https://github.com/gayanW/compile-this-1
.. in case someone could spare a moment to try out my commands (copy-paste will do). It has the same directory structure as the above.
If you could tell me how to compile the two classes in there, that would be more than enough. However the only requirement is that MyClass.class
should go in build/CUSTOM
whereas Matcher.class
should go in the build/PATCH
directory.
Note:
- This is just for testing purposes and will not be used in production.
com.example.MyClass
which I still wouldn't assumingly correlate since the structure shared by you hides the package structure without any information. – Nobleminded-class-path build/custom
when patching a class. Because my Matcher.java have a reference to classes that are compiled intobuild/custom
– Unprofitable--add-modules java.base=ALL-UNNAMED
. In other to run then you'll also need to put com.example.MyClass on the boot class path with -Xbootclasspath/a. – Hijacker-d
,-cp
,--patch-module
, and--add-reads
– Unprofitable