Re: How are multiple Java files compiled together?
Jason Kim wrote:
Knute Johnson wrote:
Jason Kim wrote:
I am new to Java.
Here are two files.
VolcanoApplication.java
class VolcanoApplication {
Get used to declaring top-level classes 'public':
public class VolcanoApplication
Once in a while you don't, but really almost never.
(The top-level class is the one whose name matches the file name.)
public static void main(String[] arguments) {
VolcanoRobot dante = new VolcanoRobot();
As you have learned, the "javac" compiler is smart enough to recognize the
reference to 'VolcanoRobot' and hunt down the source or class.
You absolutely do have to give the compiler some help. This help comes in two
forms: "sourcepath" and "classpath". (The JVM runner, "java", also knows about
"classpath".)
In your case, without any other specification, both paths by default are ".",
that means the current directory.
So when the compiler hunts the classpath, it looks in the current directory.
The first time it does not find "VolcanoRobot.class" in the classpath, so it
hunts for "VolcanoRobot.java" in the sourcepath. In your case, it found it
there, compiled it, then used the resulting "VolcanoRobot.class" in the classpath.
Next time you compile, if you have made no source changes that are newer than
the class files, it will skip the step.
If you compile just 'VolcanoApplication', and there are no newer changes to
'VolcanoRobot' the compiler will find the class file and be happy. If the
source is newer, the compiler will recompile "VolcanoRobot.java", then find
the class file and be happy.
....
}
VolcanoRobot.java
class VolcanoRobot {
...
}
I compiled the program by doing
$ javac VolcanoApplication.java
But how does Java know that VolcanoRobot.java should also be in the compilation?
If it didn't know, think about what a pain that would be every time you
had to compile a program with a lot of files. You reference
VolcanoRobot in VolcanoApplication, the compiler looks for that that
class in the classpath and compiles it if necessary.
That feature can be the source of a very tricky problem and that is if
there is a VolcanoRobot.class file in the classpath the compiler won't
compile VolcanoRobot.java even if it has changed since the
VolcanoRobot.class file was created.
That is not always correct, only if the compiler can't find the source or if
the change is limited to compile-time constants.
"Note: Classes found through the classpath may be subject to automatic
recompilation if their sources are also found. See Searching For Types."
<http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/javac.html>
I didn't know Java compiler could be that smart.
As for the potential problem you mentioned, I don't think I'll have to worry
about it any time soon. I won't be programming anything crazy complicated for some time.
Never give yourself such an excuse to defer learning.
How do you know, as a self-admitted beginner, what you do and do not need to
learn?
As it happens, understanding how the compiler and JVM invoker work are among
the very first and most important things you should learn. It isn't about
whether what you do will be "crazy complicated", or even slightly complicated,
or even dog simple. It's about whether you can do anything at all.
<http://docs.oracle.com/javase/6/docs/technotes/tools/>
P.S., Don't quote sigs.
--
Lew
This is a sig (short for "signature"). It does not convey part of the main
conversation. No need to quote it.