How does java compiler find classes without header files?

When we refer to the className class in jar, how does it know if it was defined or not when there are no header files (for example, in c/c++ )?

+4
source share
5 answers

When you run the Java compiler or your application, you can specify the class path, which lists all the jars and directories from which you load the classes. The jar just contains a bunch of class files; these files have enough metadata in them that do not require additional header files.

+2
source

Java works with class loaders . Classes are needed for compilation, as it will perform a static type check to make sure that you use the correct signatures for each method.

However, after compiling them, they are not connected, as you have, in the C / C ++ compiler, so each .class file is autonomous. Of course, this means that you will have to provide the compiled classes used by your program when you intend to execute it. So this is a little different than the way C and C ++ prepare executables. You actually do not have a binding phase, this is not necessary.

the classloader will dynamically load them, adding them to the runtime database used by the JVM.

In fact, there are many class loaders that are used by the JVM that have different permissions and properties, you can also explicitly call it to ask to load the class. What happens can also be a kind of β€œlazy” loading, in which the compiled .class code is loaded only if necessary (and this loading process can raise a ClassNotFoundException if the specified class is not inside the class path)

+2
source

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

When compiling multiple classes, javac is smart enough to automatically compile dependencies, so the system still works.

+1
source

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

0
source

Java files are compiled into class files, which are java bytecode. These class files are located in the file structure, where the classpath variable is indicated at the top level. Compilation in C / C ++ creates object files that can be associated with executable binary files. Java only compiles to bytecode files that are loaded by the JVM at runtime. The following are further explanations.

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

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

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

0
source

All Articles