views:

91

answers:

5

When we refer to a class className in jar, how does it know whether it's defined or not when there's no header files(like in c/c++) ?

A: 

It looks at the classpath and tries to load the class from there to get its definition.

Bruno
So there's no separate process of **compile** and **link** in java?
ollydbg
@ollydbg: Yes. The linking takes place at runtime.
musiKk
@ollydbg: There are separate processes, but there's more of an overlap between the information each step needs.
David Thornley
As @musiKk said, it doesn't really work like compiling and linking in C or C++, the "linking" is at runtime.
Bruno
+1  A: 

When you run the Java compiler or your application itself, you can specify a classpath which lists all the jars and directories you're loading classes from. A jar just contains a bunch of class files; these files have enough metadata in them that no extra header files are necessary.

Chris Jester-Young
+1  A: 

The classes in the jar file contain all the required information (class names, method signatures etc) so header files are not needed.

When you compile multiple classes javac is clever enough to compile dependencies automatically so the system still works.

Cameron Skinner
I used to know a Cameron Skinner who used to go to University of Auckland, and had some involvement with the Tramping Club crowd. Are you the same Cameron Skinner? Curious. :-)
Chris Jester-Young
Yup, that's me :)
Cameron Skinner
+1  A: 

Java works with classloaders. Classes are needed for compilation, since it will perform static type checking to ensure that you are using the correct signatures of every method.

After compiling them, though, they are not linked like you have in a C/C++ compiler so basically every .class file is standalone. Of course this means that you will have to provide compiled classed used by your program when you are going to execute it. So it's a little bit different from how C and C++ prepare executables. You don't actually have a linking phase at all, it is not needed.

The classloader will dinamically load them by adding them to the runtime base used by the JVM.

Actually there are many classloaders that are used by the JVM that have different permissions and properties, you can also invoke it explicitly to ask for a class to be loaded. What happens can also be a sort of "lazy" loading in which the compiled .class code is loaded just when needed (and this loading process can throw a ClassNotFoundException if the asked class is not inside the classpath)

Jack
A: 

Java files are compiled into class files which are java bytecode. These class files reside in a file structure where the top level is pointed to by the classpath variable. Compiling in C/C++ creates object files which can be linked into executable binaries. Java only compiles into bytecode files which are pulled in by the JVM at runtime. The following provide more explanation.

http://en.wikipedia.org/wiki/Java_bytecode

http://en.wikipedia.org/wiki/Java_compiler

http://en.wikipedia.org/wiki/Java_Virtual_Machine

dharga