This week's book giveaway is in the Spring forum.
We're giving away four copies of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 and have Marten Deinum & Iuliana Cosmina on-line!
See this thread for details.
Win a copy of Pro Spring MVC with WebFlux: Web Development in Spring Framework 5 and Spring Boot 2 this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic