views:

80

answers:

2

two questions:

  • how do i compile a .java file that isn't on my username (like something in documents or some other sub folder)
  • if i have multiple .java files and i compile one that contains method that are contained in the others does the compiler compile those other files.

heres an example of the second question.

example1.java:

class example1
{ 
main () {
    example2.method () }
}

example2.java:

class example2 
{  
     method () 
} 

When i compile example1.java will example2.java also be compiled?

+2  A: 

Java can find referenced files in two ways: from the source path or the class path.

The source path tells javac where to find .java source files. If it needs to compile them it will. It will put them in the designated output directory.

The class path tells javac where to find .class files for compiled Java classes. Java will use these to resolve external references (like example2).

So in your case, Java will use an existing example2 if it can find one in the class path. If it can't it will find one in the source path and compile it (it will also do this if the class file exists under certain circumstances too). If it can't find it in the source path, it's a compile error.

cletus
so what do i do in terminal?
David
you can set the classpath with the -cp flag, then pass in the list of directories to search in, the syntax is similar to the colon delimited syntax for setting your system PATH variable
Michael
well i don't know how to do that eather so why don't you write an answer that makes everything explicit and i'll throw an upbote or two your way.
David
A: 

You are prudent to learn how to do this from the command line. @Cletus' answer is correct, but you may like to study the examples in the javac documentation. The documentation written for Solaris, but it applies to Linux and BSD derivatives, such as Mac OS X. This guide to Common Problems (and Their Solutions) may be helpful, too.

trashgod
so you didn't answer the question you just told me answers exist somewhere. I think we can assume i already know this since it would be obserd of me to ask a question i think isn't answerable.
David
@David: You misspelled "absurd".
trashgod
that doesn't matter
David
Is that you down-voting my old questions today?
trashgod
what question? you only have an answer in here.
David