• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Leonard Fischer
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic