aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Classpath setting involving java and javac Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Classpath setting involving java and javac" Watch "Classpath setting involving java and javac" New topic
Author

Classpath setting involving java and javac

Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
EDIT: Don't waste your time reading through this. Read post #4 to see what was going on.

I thought I understood how this worked, but apparently not. Here is my test case:



I change directory to test, and:
javac xcom/B.java // Compiles successfully A.class and B.class
java xcom.B // Successfully executes B's main method

But why does this work? I checked my CLASSPATH environment variable, and it is unset. Why are the operations above working? How are both javac and java finding ycom.A? I thought that the current directory was never implicitly included in the search path for java and javac, so this contradicts what I thought I knew about these commands regarding classpaths.

I'm running this from a Linux machine, here is my output of java -version:
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06)
Java HotSpot(TM) Client VM (build 10.0-b23, mixed mode, sharing)

Thanks in advance for any clarification.


All code in my posts, unless a source is explicitly mentioned, is my own.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

The thing which i learnt from the other topic was that...
When we write statement package ycom as in the below program from that point onwards class A can only be identified as ycom.A.



A similar discussion was there here


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I understand that, James. The fact is that a package becomes part of the fully qualified name of the class, and the fully qualified name of the class must also be reflected in the directory structure. But that has nothing to do with my question. How are java and javac finding the ycom.A class in my example? After all, the test directory is not set in the classpath, and the current directory (according to K&B) is not implicitly included in the classpath. The only thing that the current directory is used for is to search the source files that you specify in the javac use, like:
javac B.java // B.java will be searched in the current directory
Asides from that, I thought the current directory wasn't used implicitly for anything.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
OK...After much hair pulling, I have found that javac and java use the current directory when no classpath option is used in the command line and no CLASSPATH environment variable is set.
http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html
http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html

It was a silly confusion really, because if the classpath weren't set to the current directory by default then
java SomeClass
would never work. :lol:
What K&B mean is that if either the CLASSPATH is set, or if the -classpath command line option is used, then you must include the current directory explicitly if you want it to be part of the classpath.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Those links tells some interesting facts ! !
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
James Tharakan wrote:Those links tells some interesting facts ! !

Yes, James. They sure do. But it was mostly a silly confusion. K&B is not wrong.
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Hey Ruben,
I had never set my CLASSPATH before( even now i did not ). till today i always had a trouble in importing class and setting up the class path.Now as we came to know about the fact that "If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory", ALL MY DOUBTS CLEARED.


For the last couple of hours i tried all possible combination of import statements ,classpaths and current DIRs.
Everything compiled and was running in my first try.

Thank you Ruben .And also to punit and Sachin Adat
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ruben Soto wrote:OK...After much hair pulling, I have found that javac and java use the current directory when no classpath option is used in the command line and no CLASSPATH environment variable is set.
http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html
http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html

It was a silly confusion really, because if the classpath weren't set to the current directory by default then
java SomeClass
would never work. :lol:
What K&B mean is that if either the CLASSPATH is set, or if the -classpath command line option is used, then you must include the current directory explicitly if you want it to be part of the classpath.


In my case Ruben.
I did not use -cp or -classpath but my CLASSPATH environment variable is set, but running B.class with

java xcom/B


is able to find ycom/A class


F:\workspace\scjp\foo\test>javac -d . A.java B.java

F:\workspace\scjp\foo\test>java xcom/B
Compiled

F:\workspace\scjp\foo\test>echo %CLASSPATH%
C:\apache-tomcat-6.0.14\classes\lib\axis-url.jar;C:\apache-tomcat-6.0.14\common\
lib\servlet-api.jar;D:\Program Files\Java\jdk1.6.0_10\include;D:\Program Files\J
ava\jdk1.6.0_10\include\win32;D:\Program Files\Java\jdk1.6.0_10\lib\tools.jar;D:
\bea\server\lib\weblogic.jar;


SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
James Tharakan wrote:Hey Ruben,
I had never set my CLASSPATH before( even now i did not ). till today i always had a trouble in importing class and setting up the class path.Now as we came to know about the fact that "If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory", ALL MY DOUBTS CLEARED.


For the last couple of hours i tried all possible combination of import statements ,classpaths and current DIRs.
Everything compiled and was running in my first try.

Thank you Ruben .And also to punit and Sachin Adat

Good to know, James. At least we know that all our efforts were fruitful.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Punit, do you say that you have your CLASSPATH set, and it doesn't contain the current directory, and even so you can compile and run the code? Now, this is a mystery. It doesn't make any sense.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ruben Soto wrote:Punit, do you say that you have your CLASSPATH set, and it doesn't contain the current directory, and even so you can compile and run the code? Now, this is a mystery. It doesn't make any sense.


I have windows xp Ruben, let this thing confirmed by Ankit and Sachin also.

Where are you Ankit and Sachin? Just take a look here also.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Punit,
you have put the .java files in test directory ...... right?
Assuming the above,
When you try,
javac -d . A.java B.java
the compiler tries to find the above files in the test directory, which it finds. It compiles A.java first(I am not sure why),maybe because it wants class A to compile B.
since you use -d it will create the directory for class A, so it creates the folder ycom and then puts A.class in it.
After this it tries to compile B.java, which gets ycom.A and it compiles too and a folder xcom is created and B.class is put into it.
But, suppose you remove A.java from your above command then you are sure to get an error.(All this if you don't have anything other than the 2source files in test)
This is because it is looking for ycom.A, the class or the source file A.java in the folder ycom which it cannot find. If you put the source files in their respective folders it will start behaving normally w.r.t classpath issues.

Still I am a little confused at one of the lines from the sites that Ruben sent............. :roll:
If the -sourcepath option is not specified, the user class path is also searched for source files.

I am thinking about this..... :roll:
I am gonna go mad, if there is some more fun..............


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952

F:\workspace\scjp\foo\test>javac ycom/A.java

F:\workspace\scjp\foo\test>javac xcom/B.java

F:\workspace\scjp\foo\test>java xcom/B
Compiled

F:\workspace\scjp\foo\test>echo %classpath%
C:\apache-tomcat-6.0.14\classes\lib\axis-url.jar;C:\apache-tomcat-6.0.14\common\
lib\servlet-api.jar;D:\Program Files\Java\jdk1.6.0_10\include;D:\Program Files\J
ava\jdk1.6.0_10\include\win32;D:\Program Files\Java\jdk1.6.0_10\lib\tools.jar;D:
\bea\server\lib\weblogic.jar;


The doubt is not what you answered Sachin. I have now put A.java and B.java in their corresponding package folder.

Actually I have neither used -cp, -classpath nor my CLASSPATH environment variable is set to current directory, still running with java xcom/B find ycom/A in current directory. That is contradictory to what said by Ruben in his R&D.
It was a silly confusion really, because if the classpath weren't set to the current directory by default then
java SomeClass
would never work.
What K&B mean is that if either the CLASSPATH is set, or if the -classpath command line option is used, then you must include the current directory explicitly if you want it to be part of the classpath.


We are trying to be clear on this point.


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Punit Singh wrote:
Actually I have neither used -cp, -classpath nor my CLASSPATH environment variable is set to current directory, still running with java xcom/B find ycom/A in current directory. That is contradictory to what said by Ruben in his R&D.

Actually, that's exactly the way things seem to work. If you are not using -cp (or -classpath) and CLASSPATH is not set, then when you run java and javac, it is as if the classpath only contains the current directory. But if you set the classpath either by the environment variable or with the command line option, then if you want the current directory, it must be included explicitly.

I think I was getting confused earlier because I didn't get that point, but now it makes sense. I think your results, as well as Sachin's and James' results confirm this also, don't they?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ruben Soto wrote:
Punit Singh wrote:
Actually I have neither used -cp, -classpath nor my CLASSPATH environment variable is set to current directory, still running with java xcom/B find ycom/A in current directory. That is contradictory to what said by Ruben in his R&D.

Actually, that's exactly the way things seem to work. If you are not using -cp (or -classpath) and CLASSPATH is not set, then when you run java and javac, it is as if the classpath only contains the current directory. But if you set the classpath either by the environment variable or with the command line option, then if you want the current directory, it must be included explicitly.

I think I was getting confused earlier because I didn't get that point, but now it makes sense. I think your results, as well as Sachin's and James' results confirm this also, don't they?


I mean my CLASSPATH environment variable is not set to current directory, but it is set to other things.

see:

F:\workspace\scjp\foo\test>echo %classpath%
C:\apache-tomcat-6.0.14\classes\lib\axis-url.jar;C:\apache-tomcat-6.0.14\common\
lib\servlet-api.jar;D:\Program Files\Java\jdk1.6.0_10\include;D:\Program Files\J
ava\jdk1.6.0_10\include\win32;D:\Program Files\Java\jdk1.6.0_10\lib\tools.jar;D:
\bea\server\lib\weblogic.jar;


Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
That's really strange... Are you running through Eclipse only, or does this also happen when running from the console?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ruben Soto wrote:That's really strange... Are you running through Eclipse only, or does this also happen when running from the console?


I am only using command prompt and Edit Plus.

I have shown you my command prompt outputs.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
I still thought you were using Eclipse, and maybe it was doing something on its own. I really don't know why things are working for you then.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ruben Soto wrote:I still thought you were using Eclipse, and maybe it was doing something on its own. I really don't know why things are working for you then.


Nope, I am command prompt only, where is Ankit and Sachine, tell them to try this on command prompt.

See this Ruben, all these are command prompt outputs, difference is only that I have Windows xp.


Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Hi Punit,
As you had earlier written
Punit Singh wrote:
F:\workspace\scjp\foo\test>javac -d . A.java B.java
F:\workspace\scjp\foo\test>java xcom/B
Compiled

I thought that you are not giving the complete path to B.java. So I assumed that you didn't put the files in their respective package folders.

Punit Singh wrote:F:\workspace\scjp\foo\test>javac xcom/B.java

This will surely work because

marc weber wrote:"You should not need to set a classpath.
Java can find the API classes itself, and the classpath defaults to the current directory for classes you've compiled yourself."
here http://www.coderanch.com/t/407823/Java-General-beginner/classpath-command#1793011

Jesper Young wrote:
"you should not set the CLASSPATH environment variable at all. If it is not set, Java by default looks in the current directory for class files to run."
here http://www.coderanch.com/t/404486/Java-General-beginner/Problems-compiling-programcan-anyone-help#1776067

Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Punit Singh wrote:
I mean my CLASSPATH environment variable is not set to current directory, but it is set to other things.

F:\workspace\scjp\foo\test>echo %classpath%
C:\apache-tomcat-6.0.14\classes\lib\axis-url.jar;C:\apache-tomcat-6.0.14\common\
lib\servlet-api.jar;D:\Program Files\Java\jdk1.6.0_10\include;D:\Program Files\J
ava\jdk1.6.0_10\include\win32;D:\Program Files\Java\jdk1.6.0_10\lib\tools.jar;D:
\bea\server\lib\weblogic.jar;

But I guess, it gets all the API files it wants...........
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Sachin Adat wrote:
Punit Singh wrote:F:\workspace\scjp\foo\test>javac xcom/B.java

This will surely work because

marc weber wrote:"You should not need to set a classpath.
Java can find the API classes itself, and the classpath defaults to the current directory for classes you've compiled yourself."
here http://www.coderanch.com/t/407823/Java-General-beginner/classpath-command#1793011

Jesper Young wrote:
"you should not set the CLASSPATH environment variable at all. If it is not set, Java by default looks in the current directory for class files to run."
here http://www.coderanch.com/t/404486/Java-General-beginner/Problems-compiling-programcan-anyone-help#1776067



Same thing will apply to java command also na. No need to set current directory anywhere na, if you have not used -classpath/-cp to set it to other directory, but a big but CLASSPATH environment variable could be set to anyother directories.
F:\workspace\scjp\foo\test>java xcom/B
Compiled


And what about these research? Any final conclusion?
OK...After much hair pulling, I have found that javac and java use the current directory when no classpath option is used in the command line and no CLASSPATH environment variable is set.
http://java.sun.com/javase/6/docs/technotes/tools/windows/javac.html
http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html

It was a silly confusion really, because if the classpath weren't set to the current directory by default then
java SomeClass
would never work.
What K&B mean is that if either the CLASSPATH is set, or if the -classpath command line option is used, then you must include the current directory explicitly if you want it to be part of the classpath.


Guys make this programs on your machine and confirm this.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
I agree with Ruben here.
I guess there is something in your classpath variables which is setting it to the current directory...... maybe tools.jar or maybe something else, I am not sure.

Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Sachin Adat wrote:I agree with Ruben here.
I guess there is something in your classpath variables which is setting it to the current directory...... maybe tools.jar or maybe something else, I am not sure.



Ohhh, than let me delete that CLASSPATH environment variable, and set to something nonsense and try.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Sachin Adat wrote:I agree with Ruben here.
I guess there is something in your classpath variables which is setting it to the current directory...... maybe tools.jar or maybe something else, I am not sure.



Ok you were right Sachin. I did all cleaning and Set CLASSPATH environment variable to c:\ and compiled as like shown below:

F:\workspace\scjp\foo\test>echo %CLASSPATH%
c:\

F:\workspace\scjp\foo\test>javac ycom/A.java

F:\workspace\scjp\foo\test>javac xcom/B.java
xcom\B.java:2: package ycom does not exist
import ycom.A;
^
xcom\B.java:3: cannot find symbol
symbol: class A
public class B extends A{
^
2 errors


So it did not get current directory by default.
Then I set again all the CLASSPATH, like tools.jar etc....
And compiled and run:
F:\workspace\scjp\foo\test>echo %CLASSPATH%
C:\apache-tomcat-6.0.14\classes\lib\axis-url.jar;C:\apache-tomcat-6.0.14\common\
lib\servlet-api.jar;D:\Program Files\Java\jdk1.6.0_10\include;D:\Program Files\J
ava\jdk1.6.0_10\include\win32;D:\Program Files\Java\jdk1.6.0_10\lib\tools.jar;D:
\bea\server\lib\weblogic.jar;

F:\workspace\scjp\foo\test>javac ycom/A.java

F:\workspace\scjp\foo\test>javac xcom/B.java

F:\workspace\scjp\foo\test>java xcom/B
Compiled


Then I deleted that CLASSPATH environment variable and did the same:
F:\workspace\scjp\foo\test>javac ycom/A.java

F:\workspace\scjp\foo\test>javac xcom/B.java

F:\workspace\scjp\foo\test>java xcom/B
Compiled


Now everything clear. It was something in CLASSPATH environment variable that was playing hide and seek, may be its %JAVA_HOME%\lib\tools.jar.

Thanks buddy

[EDIT: Finally I deleted CLASSPATH environment variable for forever, so in future I did not get any conflicts. ]

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Thank god... NO MORE CONFUSION
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
James Tharakan wrote:Thank god... NO MORE CONFUSION

When I had replied in the first post, I thought I knew everything(I even got the self test question right).
Then as the messages started increasing, I thought maybe I don't know much.
After some time maybe I don't know anything, and the confidence was just downhill.
Suddenly it started to rise...........I won't say I know everything now, but still I know much more than I started......still maybe just halfway.

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Sachin Adat wrote:I won't say I know everything now, but still I know much more than I started......still maybe just halfway.



May be you are right
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Go after confusion, once in a time it will be solved, we are fighters guys.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Good job guys! That was a sneaky way that your classpath was getting set, Punit. No wonder we couldn't figure it out.

I agree with Sachin. In my case I realized I didn't know as much as I thought I did in that other thread about question 3 in K&B where Punit mentioned that a case that didn't explicitly contain a classpath was compiling and running fine. That got me thinking.
But it's all OK now. "The path to the palace of wisdom leads through the forest of confusion." :lol:
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Ruben Soto wrote:"The path to the palace of wisdom leads through the forest of confusion." :lol:

Hope I will be in the palace rather than the forest, while giving the exam.............
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Classpath setting involving java and javac