This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Some questions about javac and java commands. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Some questions about javac and java commands." Watch "Some questions about javac and java commands." New topic

Some questions about javac and java commands.

Dmitry Zhuravlev
Ranch Hand

Joined: Apr 14, 2010
Posts: 93
Guys, could you explain me some features of java and javac commands, please.

(1) I have a compiled package named 'solutiondatabase' based in the following folder: C:\Documents and Settings\MyName\workspace\SolutionDatabase\src\MyFiles
Consequently, 'solutiondatabase' folder with class files exists in MyFiles folder.
I want to run Main class of that package using 'java' command from the SolutionDatabase folder.

This command works:
java -cp src/MyFiles solutiondatabase.Main

My question is: can I do the same without classpath argument via specifying exact path to the compiled class? Can I do the same in case there are no package and I need to run only one compiled file?
I tried this, but it doesn't work:
java src/MyFiles/solutiondatabase/Main

(2) Here is a quote from the SCJP preparation guide: very common situation occurs in which java or javac complains that it can't find a class file, and yet you can see that the file is IN the current directory! When searching for class files, the java and javac commands don't search the current directory by default.
hmm.. If I go to MyFiles folder (which contains compiled solutiondatabase folder with class files) and run - it works. javac works the same way. So, whether it really searches in the current folder, or not?

How can I run the compiled class files if I am exactly in the folder with that files, i.e. in the folder with the same name as my package (solutiondatabase) ?

(3) When specifying classpath, how can I input a folder two levels higher than current one?
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
1) If your CLASSPATH system variable includes . (a dot, it represents currnet directory) and you're in C:\Documents and Settings\MyName\workspace\SolutionDatabase\src\MyFiles then command

java Main

should work. Look up how to set your CLASSPATH

2) As described above, if you have CLASSPATH set to include your current directory it works. But CLASSPATH dosen't include current dir by default.

3) ./ points to your curreent directory ../ (two dots and a slash) points to one level higher. So ../../ points two levels higher.

Note, that the package of your class should correspond to the directory it is in. In you case, either make package of Main class MyFiles or rename directory MyFiles to solutiondatabase.



I no good English.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43900
Martin Vanyavchich wrote:But CLASSPATH dosen't include current dir by default.
That is true, but slightly confusing. If there is no system CLASSPATH set, the "java" and "javac" tools will assume the CLASSPATH is "." ie the current directory. There is therefore often no need to set a CLASSPATH variable. A lot of people who post here appear to do more harm than good by setting a system CLASSPATH.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

I'd go a little stronger and say that that particular study guide is wrong, and I'd be suspicious of anything else it might say. The only default behavior is the behavior when CLASSPATH is unset, as no jdk installer sets it. And as was just stated, without CLASSPATH the effective class path is "." .

[Jess in Action][AskingGoodQuestions]
Dmitry Zhuravlev
Ranch Hand

Joined: Apr 14, 2010
Posts: 93
Guys, thank you all for the replies!

From this I can conclude the following:
- its impossible to run class from a package if class file is not located in the folder named packagename.
- if I want to run a class file from a package and I am in the folder with that file, to run it I'll have to include upper folder in the classpath and go from there: i.e. call java -cp ../ packagename.filename
- CLASSPATH variable cannot affect two previous statements
- if I use -cp parameter the current folder will not be searched, except for explicitly introducing it as '.'

Am I right?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24189

All of these statements are correct.
I agree. Here's the link:
subject: Some questions about javac and java commands.
It's not a secret anymore!