How to use --patch-module along with --class-path | javac
Asked Answered
U

0

6

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.
Unprofitable answered 8/3, 2018 at 14:51 Comment(12)
Just guessing you've edited the names manually, your exception might be correct when it would be read com.example.MyClass which I still wouldn't assumingly correlate since the structure shared by you hides the package structure without any information.Nobleminded
I don't get it. what do you mean by edited manually? I created directories based on packages, but that does not make any difference. All I want is to use a different classpath than build.Unprofitable
What I meant was that if you re-read your question, you'll find the exception doesn't correlate to the other details shared by you and nor does the project and package structure match with each other.Nobleminded
What I'm saying is that I want to use different class path like -class-path build/custom when patching a class. Because my Matcher.java have a reference to classes that are compiled into build/customUnprofitable
@nullpointer please see the updated post.Unprofitable
I assume you are looking for --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
@AlanBateman could you have a look at the test repo :) please see the updated post.Unprofitable
There's a typo in my command, I meant --add-reads (not --add-modules). I don't have time to study the test repo.Hijacker
@AlanBateman wow. thanks. finally go it to work. Had to use all -d, -cp, --patch-module, and --add-readsUnprofitable
@AlanBateman feel free to add an answer.Unprofitable
@GayanWeerakutti This is the part where you add an answer, then accept it. You may have to wait 24 hours between answer and acceptance.Personification
@GayanWeerakutti What's the solution?Halfassed

© 2022 - 2024 — McMap. All rights reserved.