Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #14849

Re: How are multiple Java files compiled together?

From Lew <noone@lewscanon.com>
Newsgroups comp.lang.java.programmer
Subject Re: How are multiple Java files compiled together?
Date 2012-05-27 20:18 -0700
Organization albasani.net
Message-ID <jpuqqg$md9$1@news.albasani.net> (permalink)
References <90a3f6ec-dfd4-472b-866d-98eaa92364d5@googlegroups.com> <jpudmj$m0h$1@dont-email.me> <19fc754b-0501-46dc-ac81-3589c708e004@googlegroups.com>

Show all headers | View raw


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.

Back to comp.lang.java.programmer | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How are multiple Java files compiled together? Jason Kim <iamjsonkim@gmail.com> - 2012-05-27 15:48 -0700
  Re: How are multiple Java files compiled together? Knute Johnson <nospam@knutejohnson.com> - 2012-05-27 16:34 -0700
    Re: How are multiple Java files compiled together? Jason Kim <iamjsonkim@gmail.com> - 2012-05-27 16:47 -0700
      Re: How are multiple Java files compiled together? Lew <noone@lewscanon.com> - 2012-05-27 20:18 -0700
        Re: How are multiple Java files compiled together? Knute Johnson <nospam@knutejohnson.com> - 2012-05-27 21:08 -0700
          Re: How are multiple Java files compiled together? Gene Wirchenko <genew@ocis.net> - 2012-05-28 09:32 -0700
            Re: How are multiple Java files compiled together? Lew <noone@lewscanon.com> - 2012-05-28 22:35 -0700
              Re: How are multiple Java files compiled together? Gene Wirchenko <genew@ocis.net> - 2012-05-29 09:20 -0700
  Re: How are multiple Java files compiled together? Roedy Green <see_website@mindprod.com.invalid> - 2012-05-27 17:35 -0700
    Re: How are multiple Java files compiled together? Roedy Green <see_website@mindprod.com.invalid> - 2012-05-27 19:34 -0700
    Re: How are multiple Java files compiled together? Roedy Green <see_website@mindprod.com.invalid> - 2012-05-28 04:00 -0700
      Re: How are multiple Java files compiled together? Lew <noone@lewscanon.com> - 2012-05-28 22:56 -0700

csiph-web