wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes I need Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "I need "-cp ." on my javac command, but the OS classpath includes my current folder" Watch "I need "-cp ." on my javac command, but the OS classpath includes my current folder" New topic
Author

I need "-cp ." on my javac command, but the OS classpath includes my current folder

Leonard Fischer
Ranch Hand

Joined: Apr 17, 2009
Posts: 47
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...

armando fonseca
Ranch Hand

Joined: Apr 03, 2009
Posts: 49
Hi leonard,

I just tried your example and I got no problem. I didn't need to use "-cp ." to compile User.java I just used javac User.java.
I'm running linux, but I don't think this will be an OS issue.
The JDK looks for classes in this way:
1. the java standard libraries,
2. the classpath,
3. your current directory,

So doing -cp . will be redundant as you pointed out. To be honest, I don't think you missed something....


scjp6-90%
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: I need "-cp ." on my javac command, but the OS classpath includes my current folder