Compilation of the Java 8 JDK source code with javac command line compiler

608 views Asked by At

Could you please advise how the JDK source files can be compiled with debug information using Javac.

I am using the following command:

javac -J-Xms16m -J-Xmx1024m -source 8 -target 8 -sourcepath d:\jdk_src -classpath ".:rt.jar:javax-crypto.jar:ui.jar" -bootclasspath ".:rt.jar:javax-crypto.jar:ui.jar" -d "jdk_debug" -g @filelist.txt >> log.txt 2>&1

Steps to reproduce:

  • source and target version is Java 8
  • sourcepath holds the directory of all the java classes (extracted from src.zip found in JDK/)
  • rt.jar is found in JDK_HOME\jre\lib and holds the required classes that the source code must be compiled against
  • filelist.txt contains all the classes found in the src.zip file that must be compiled (extracted using the command dir /B /S /X jdk_src\*.java > filelist.txt
  • we are outputing the logs in log.txt

Instead of the classes being compiled successfully, errors like the following are being raised:

 \jdk\jdk_src\java\lang\reflect\AccessibleObject.java:29: error: package sun.reflect does not exist import sun.reflect.Reflection;
 \jdk\jdk_src\java\lang\reflect\AccessibleObject.java:30: error: package sun.reflect does not exist import sun.reflect.ReflectionFactory;
 \jdk\jdk_src\java\util\jar\Attributes.java:37: error: package >sun.util.logging does not exist

These packages are present in the provided jars (rt.jar, ui.jar and javax-crypto.jar). Could you please explain why the source code is not being compiled since the classpath files are given to the compiler?

Any feedback will be appreciated.

Thank you.

1

There are 1 answers

3
Stephen C On BEST ANSWER

The Java source code is not designed to be built that way. What to should do is to checkout the OpenJDK source tree and follow the build instructions in the source tree. You should be able to tweak the build scripts to compile with debug information ... and either use the built "rt.jar" directly or extract the classes that you need from the build artifacts.

You can get the Java 8 source code from Github: https://github.com/openjdk/jdk8u. (Other versions are nearby ...) Select the git tag corresponding to the Java version that you want to build. The links to the build instructions are in the README.md file.

It is not entirely clear why the compilation is failing when you do it the way you are doing. However, the javac compiler implements some restrictions that are designed to stop you compiling ordinary application code against internal APIs in the "rt.jar". Some of the "src.zip" code you are compiling (legitimately) depend on the internal APIs.