I hope this doesn't waste anyone's time; I suspect I'm missing something.
I compiled a small
Java file with a package declaration. I compiled it with "-d ." so the .class file went into a subdirectory of my current folder.
Then, I tried to compile another Java file, which does import static on members of the first Java file. I discovered that to compile this second file, I had to say "-cp ." in the javac command.
I don't mind saying "-cp .", but I'm surprised, since when I echo %CLASSPATH%, I see that the OS classpath includes the folder I'm currently in.
What I did is below. I'll use "//" to provide "comments".
C:\JAVA16\QuizA>javac -d . Stuff.java // compile the Java file whose first line is "package tweet;"
C:\JAVA16\QuizA>javac User.java // try to compile the second Java file, which does import static on stuff in the first file
User.java:1: package tweet does not exist
import static tweet.Stuff.*;
^
User.java:7: cannot find symbol
symbol : variable MY_CONSTANT
location: class User
void go() { out.println(doStuff(MY_CONSTANT)); }
^
2 errors
C:\JAVA16\QuizA>javac -cp . User.java // do the same thing I just did above, except now, use "-cp ." It works!
C:\JAVA16\QuizA>echo %CLASSPATH%
"C:\Program Files\Java\jdk1.6.0_11\bin;C:\JAVA16;C:\JAVA16\QuizA" // here's what surprises me: the OS classpath includes the folder I'm currently in.
C:\JAVA16\QuizA>dir tweet // just checking to make sure Stuff.class got into the tweet subfolder.
Volume in drive C has no label.
Volume Serial Number is FC70-C5D5
Directory of C:\JAVA16\QuizA\tweet
04/19/2009 02:04 PM <DIR> .
04/19/2009 02:04 PM <DIR> ..
04/19/2009 02:04 PM 342 Stuff.class
1 File(s) 342 bytes
2 Dir(s) 35,721,617,408 bytes free
C:\JAVA16\QuizA>
Let me also list the two files.
Stuff.java:
package tweet;
public class Stuff {
public static int MY_CONSTANT = 5;
public static int doStuff(int x) { return (x++)*x; }
}
User.java:
import static tweet.Stuff.*;
import static java.lang.System.out;
class User {
public static void main(
String[] args) {
new User().go();
}
void go() { out.println(doStuff(MY_CONSTANT)); }
}
Sorry if I'm missing something obvious...