2
votes

I am just curious to know how the classes (inside rt.jar provided by Oracle) like java.lang.Object, java.lang.String were generated from the .java source files. I think, it's not possible for their javac to compile them.

I tried to compile Dummy.java containing "class Dummy{}"

lab@labb:~/Documents$ set PATH=.:$JAVA_HOME/bin

lab@labb:~/Documents$ set CLASSPATH=.

lab@labb:~/Documents$ javac -verbose Dummy.java

[parsing started RegularFileObject[Dummy.java]]

[parsing completed 12ms]

[search path for source files: .]

[search path for class files: /usr/lib/jvm/java-7-oracle/jre/lib/resources.jar,/usr/lib/jvm/java-7-oracle/jre/lib/rt.jar,/usr/lib/jvm/java-7-oracle/jre/lib/sunrsasign.jar,/usr/lib/jvm/java-7-oracle/jre/lib/jsse.jar,/usr/lib/jvm/java-7-oracle/jre/lib/jce.jar,/usr/lib/jvm/java-7-oracle/jre/lib/charsets.jar,/usr/lib/jvm/java-7-oracle/jre/classes,/usr/lib/jvm/java-7-oracle/jre/lib/ext/sunpkcs11.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/dnsns.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/zipfs.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/localedata.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/sunec.jar,/usr/lib/jvm/java-7-oracle/jre/lib/ext/sunjce_provider.jar,.]

[loading ZipFileIndexFileObject[/usr/lib/jvm/java-7-oracle/lib/ct.sym(META-INF/sym/rt.jar/java/lang/Object.class)]]

[checking Dummy]

[loading ZipFileIndexFileObject[/usr/lib/jvm/java-7-oracle/lib/ct.sym(META-INF/sym/rt.jar/java/lang/AutoCloseable.class)]]

[wrote RegularFileObject[Dummy.class]] [total 131ms]

2
Why do you think it's not possible for javac to compile them?yshavit
As each & every class directly/indirectly inherits from Object. Compiler needs to make syntactic & semantic checks of the inheritance hierarchy and referenced classes.lab bhattacharjee
Perhaps you are missing the concept of a bootstrap JRE. System and javac classes are compiled with an already compiled Java library and tools. Similar the way to, say, C compilers written in C are generally compiled with the same C compiler.Tom Hawtin - tackline
So, there is a chicken-egg problem if we think of the 1st C compiler?lab bhattacharjee
@labbhattacharjee Yes. This link may help: en.wikipedia.org/wiki/Bootstrapping_(compilers)yshavit

2 Answers

2
votes

All classes can be expressed as Java code (as you can see here: http://www.docjar.com/docs/api/java/lang/package-index.html) and compiled by javac. Object is a special case insofar as when loaded by the VM, it will not get a superclass.

0
votes

You can use a decompiler (JAD by example) to see how would look source file for a .class.

In case of base classes there are some methods that need to be native and their implementation provided in system-dependent libraries like dll for Windows or so for Linux.

String has only one method native (intern). Object has more. There are classes in JRE that needs native to acomplish things related to SO and are very low-level, like atomic values or threads (thanks @yshavit), but the vast majority of the JRE library code is programmed in Java.