All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
All code in my posts, unless a source is explicitly mentioned, is my own.
All code in my posts, unless a source is explicitly mentioned, is my own.
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
James Tharakan wrote:Those links tells some interesting facts ! !
All code in my posts, unless a source is explicitly mentioned, is my own.
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 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.
java xcom/B
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
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
![]()
All code in my posts, unless a source is explicitly mentioned, is my own.
All code in my posts, unless a source is explicitly mentioned, is my own.
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.
![]()
SCJP 6
If the -sourcepath option is not specified, the user class path is also searched for source files.
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;
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.
SCJP 6
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.
All code in my posts, unless a source is explicitly mentioned, is my own.
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?
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
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:That's really strange... Are you running through Eclipse only, or does this also happen when running from the console?
SCJP 6
All code in my posts, unless a source is explicitly mentioned, is my own.
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.
SCJP 6
Punit Singh wrote:
F:\workspace\scjp\foo\test>javac -d . A.java B.java
F:\workspace\scjp\foo\test>java xcom/B
Compiled
Punit Singh wrote:F:\workspace\scjp\foo\test>javac xcom/B.java
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 https://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 https://coderanch.com/t/404486/Java-General-beginner/Problems-compiling-programcan-anyone-help#1776067
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;
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 https://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 https://coderanch.com/t/404486/Java-General-beginner/Problems-compiling-programcan-anyone-help#1776067
F:\workspace\scjp\foo\test>java xcom/B
Compiled
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.
SCJP 6
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.
SCJP 6
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.
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
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
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
SCJP 6
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
James Tharakan wrote:Thank god... NO MORE CONFUSION
![]()
Sachin Adat wrote:I won't say I know everything now, but still I know much more than I started......still maybe just halfway.
![]()
SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
SCJP 6
All code in my posts, unless a source is explicitly mentioned, is my own.
Ruben Soto wrote:"The path to the palace of wisdom leads through the forest of confusion." :lol:
Look! It's Leonardo da Vinci! And he brought a tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|