javacompilationjava-mejavac

How to compile a Java class with javac without any dependencies even on the base classes like java.lang.*?


I want to "recreate" J2ME / CLDC with a recent version of javac (e.g. 11.0.19) and compile a purposely built Java program without any or minimal base classes. Obviously classes like java.lang.Object or java.lang.String (etc.) need to be present as they are a core feature. But besides that I want to provide my own "device specific" classes (e.g. with native bindings) and use the standard javac compiler.

I already fiddled a little bit around with the options --module-source-path, -profile, --class-path (and others) but didn't achieve much. The documentation on these switches is also very "thin".

Is there a better documentation or is it even possible to get the expected result?


Solution

  • How to compile a Java class with javac without any dependencies even on the base classes like java.lang.*?

    First step would be to find or prepare a JAR file (or files) containing class files for all of the "core" classes. For J2ME / CLDC you should be able to get hold of an existing JAR file from an existing distro. Note that javac only needs the API signatures for the classes, so you could use a JAR containing "stub" classes; i.e. classes without any of the actual implementation details.

    Then you use the -bootclasspath option1 to tell javac where to find the core dependencies. For example:

    javac ... -bootclasspath /path/to/cldc.jar ... Hello.java
    

    1 - Or --boot-class-path or -Xbootclasspath. Check the javac documentation for the Java JDK version that you are using.