• Post Reply Bookmark Topic Watch Topic
  • New Topic

predefined classes  RSS feed

 
Emil Romascanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac successfully compiles the following code:



How the compiler knows what java.util.ArrayList is?
Thank you,
Emil R
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are declaring the file's location within Java - you are telling the compiler to look under java, then util, then pull out ArrayList.

If you simply put ArrayList, the compiler would look for ArrayList and return an error becuase there's no base class ArrayList - it's all under libraries.

However, it would work if you imported java.util.ArrayList first too:
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is because java.util.ArrayList class exists in rt.jar.
And rt.jar comes under Bootstrap classes.
For more information go through this
 
Emil Romascanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim Harris: import clause allows the code to reference classes by their short name, rather than full name (including package name). My question is how the compiler knows where to look for "java.util. ...". The JAVA_HOME env variable is set to the full name of the folder holding the JDK (ex: C:\Program Files\Java\jdk1.8.0_31). But in that folder there is no "util" folder. Java books state that packages shall map on directories. Probably this is true only for user code.

@Tapas Chand: my understanding is that rt.jar is used at runtime, rather than at compile time. Can you point a reference that describes how javac resolves package names or full class names?

Thank you.
Emil R
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tapas linked to your answer in the oracle documentation:
The javac and javadoc tools use class files in two distinct ways:

Like any Java application, javac and javadoc must load various class files in order to run.
To process the source code they operate on, javac and javadoc must obtain information on object types used in the source code.
The class files used to resolve source code references are mostly the same class files used to run javac and javadoc.


In other words it's loading class files based off the given jdk directory in order to figure out where the rest are (based on my understanding).
 
Emil Romascanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Tim Harris: obviously javac loads the java.util.ArrayList class. My question is how javac knows from what file to load the bytecode of java.util.ArrayList. Tapas said that it's rt.jar - and I have two comments: 1) according to its name, this seems to be loaded at runtime not at compile time; 2) if loaded at compile time, how javac knows which folder contains rt.jar? I see it in %JAVA_HOME%/jre/lib, but didn't find in any Java book that javac is looking in this folder for .jar files to be used at compile time. Maybe it does, and if so, I would like to know whether this is mentioned in a Java reference.
Thank you,
Emil R
 
Tim Holloway
Saloon Keeper
Posts: 18789
74
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I did a quick check on JDK 8 and the JDK lib directory seems to be exclusively resources used by the JDK utilities and not by applications. The jre/lib directory contains the "ordinary" resources.

Java works using classloaders, which often (but not always) include classpaths. You can write a short Java app that will allow you to display what does what by looking at the java.lang.System properties.

I'm not sure, but I think that all or part of the javac program itself is actually written in Java, so it would know just on that account. On the other hand, both the java compiler (javac) and java runtime VM (java) have the JRE lib directory hard-wired into their classloaders. Java's built-in security framework gets involved, however, so you cannot plunk just any class you want into that directory, nor can you replace the stock class resources. Not without getting your new resources properly authorized. But that's a different subject.

Long and short of it is that it Just Works. Except when it doesn't, One of the more irritating java errors you can get is when java.lang.Object cannot be found, since that's the base class for ALL java objects. You can get that message if you aren't running using the proper Java home or if you've damaged the java home directory of the JVM you are trying to run.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javac is indeed written in Java. I saw a talk at a conference recently where the presenter showed how he added 'goto' to Java by adding the ability to Javac, just for the fun of it.

Interestingly answers to this question will only apply to Java 8 and before. Java 9 will completely change the architecture of the JRE. No more rt.jar (in fact no more jars at all in the JRE), and possibly no classpath either.
 
Emil Romascanu
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you to all responders... it's not big deal. I was thinking that this could be an interview or quiz question. It seems that javac recognizes all fully qualified class names from packages starting with "java", or "javax". Probably it has some mechanism to find resolve them by using %JAVA_HOME%.
Emil R
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!