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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "java command" Watch "java command" New topic
Author

java command

Sharan Ashok Vasandani
Greenhorn

Joined: Feb 22, 2007
Posts: 12
hi,

when i am running a program,java command is checking for the class file in the current directory also by default.even if i have not set the current directory in classpath.why so?

Regards,
Sharan
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
The classpath search is specified in this documentation. Read that, and if you cannot find the answer to your question, then ask again.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you are having an actual problem then please post your CLASSPATH setting and your complete java command. You get your CLASSPATH setting with echo $CLASSPATH (unix) or echo %CLASSPATH% (windows).
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
Maybe I do something wrong, but if I try that on my machine on Windows I get
%classpath%

It just writes what I type.

After doing some playing around I discoverd that I didn't have set a classpath, because the variable echo %java_home% is working

So if windows repeats your environment variablet his means it isn't set.

I don't experience problems without my classpath setting. So why should I set it then??


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by wilhelm tell:
Maybe I do something wrong, but if I try that on my machine on Windows I get
%classpath%

It just writes what I type.

After doing some playing around I discoverd that I didn't have set a classpath, because the variable echo %java_home% is working

So if windows repeats your environment variablet his means it isn't set.

I don't experience problems without my classpath setting. So why should I set it then??


Thanks for your input Remko, but let's concentrate on Sharan's problem.
Sharan Ashok Vasandani
Greenhorn

Joined: Feb 22, 2007
Posts: 12
C:\Documents and Settings\sharanv>cd..
C:\Documents and Settings>cd..
C:\>set path=c:/jdk1.5/bin;
C:\>cd sharan C:\sharan>javac myclass.java
C:\sharan>java myclass
Hiiiii
C:\sharan>

i have not set any classpath at the command prompt,and there are no environment variables set. how is the command able to find my class file named "myclass", As per books this should not run,as java does not search for the classes in the current directory by default. then how is this working.please explain. Regards, Sharan
Sharan Ashok Vasandani
Greenhorn

Joined: Feb 22, 2007
Posts: 12
C:\sharan>echo %classpath%
%classpath%

C:\sharan>cd..

C:\>echo %classpath%
%classpath%

C:\>

Above is the value for classpath variable.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Sharan Ashok Vasandani:
...As per books this should not run,as java does not search for the classes in the current directory by default...

How old are these books?

Please review the documentation using the link Barry provided above.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Ashok Kumar
Ranch Hand

Joined: Aug 27, 2004
Posts: 93
The SUN documentation clearly states
The default class path is the current directory. Setting the CLASSPATH variable or using the -classpath command-line option overrides that default
.So unless you specify a classpath(In your case you have not) it will consider the current directory.


"Decide what you want, decide what you are willing to exchange for it. Establish your priorities and go to work."
Sharan Ashok Vasandani
Greenhorn

Joined: Feb 22, 2007
Posts: 12
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\sharanv>cd..

C:\Documents and Settings>cd..

C:\>set path=c:/jdk1.5/bin;

C:\>set classpath=c:/temp;

C:\>cd sharan

C:\sharan>java myclass
Hiiiii

C:\sharan>set classpath=c:/temp

C:\sharan>java myclass
Exception in thread "main" java.lang.NoClassDefFoundError: myclass

C:\sharan>set classpath=

C:\sharan>java myclass
Hiiiii

C:\sharan>set classpath=;

C:\sharan>java myclass
Hiiiii

C:\sharan>set classpath=c:/temp;

C:\sharan>cd..

C:\>java myclass
Exception in thread "main" java.lang.NoClassDefFoundError: myclass

C:\>set classpath=c:/temp;

C:\>cd sharan

C:\sharan>java myclass
Hiiiii

C:\sharan>

ok,agreed that defualt classpath is current directory,but then why it is not diplayed when i do echo %classpath%.

second i also tried to run the program after setting some absurd classpath,still the current directory is being searched if i ended the classpath declaration with a semicolon( ,bt if i do not end the classpath declaration with semicolon( then its not searching in the current directory.

if i suppose that it is taking the blank space after semicolon to be current directory for that i tried to set the
c:\>set classpath=c:/temp;,and then i again went to c:\sharan> and tried to run the program and yes its running.you can see that in above pasted screen.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
On windows the semicolon ";" is a path separator character - you should not be using it at the end of the path. If you wish to refer to the current directory you should use ".".

But I found this Bug Report

If you read the post at the bottom of the bug report the poster claims that an empty path element would also mean the default directory.

Also, if you search the web you will find several discussions of whether ";;" (or "::" in the case of unix) should be taken to mean "." or just be ignored.
[ February 23, 2007: Message edited by: Barry Gaunt ]
Sharan Ashok Vasandani
Greenhorn

Joined: Feb 22, 2007
Posts: 12
i read that webpage that implies even if there is no blank space purposely inserted before or after ; it will take it as a blank space and hence include current directory.
means

the following two are same
1)c:\>set classpath=;
2)c:\>set classpath= ;

my question is that even if i have not left a space after = in the 1) classpath it is same as leaving a blank space after = like i have done it in 2) classpath?why so?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java command