aspose file tools*
The moose likes Beginning Java and the fly likes Setting classpath Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Setting classpath" Watch "Setting classpath" New topic
Author

Setting classpath

Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I'm aware that some form of this question has probably been asked a million times before. However, can someone tell me how I set a classpath from DOS - so that I don't replace my existing classpath?
Why is the classpath subject so complex? It seems you can set different classpaths for different IDEs. Why is this? Do I have to reboot after resetting a classpath from DOS? Are classpaths relevant for both compiling and executing software?
So many questions......
Please help.
Paul
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

the confusion gets worse when you don't know what OS one is using.

The answer is different between Win9x and NT/2000
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Like most things, something is only complex if you don't understand it fully.
Perhaps it's best to compare classpath to your PATH environment variable on windows. If you want to be able to run programs/commands from the command line in windows, the command shell needs to know where to look for a particular command. For example, a common command is "ping". When you type "ping" the command shell looks for a program named ping.exe in all the directories you have specified in the PATH variable. If you remove the system32 directory from your path and type "ping", the command shell won't be able to locate this prgram, thus won't be able to run it for you.
Classpath is a similar concept in java. It provides a list of directories or jar files that the java tools can look in to find classes or resources. If you ask a java tool, such as javac or just the java.exe program to use a class, but haven't specified *where* to find that class, java can't find it and won't run it or compile it.
Every java tool needs the classpath specified. If you don't manually specify one with the -classpath setting, java tools will try to look at the defined environment variables for one named CLASSPATH and use that value.
So if you try to compile something like:
javac HelloWorld.java
But haven't told the javac program where to find the HelloWorld.java class, you're going to get a error: cannot read: HelloWorld.java.

If you try to run a java class like HelloWorld but don't specify a classpath , you're going to get: Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld.
This is no different than if you try to run a command in the command shell but haven't provide a PATH setting for that command. You'll get an error saying that it is not recognized as an internal or external command,
operable program or batch file.
[ March 04, 2002: Message edited by: Rob Ross ]

Rob
SCJP 1.4
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
I'm using Windows NT.
When setting a classpath in the options section of an IDE (eg. JBuilder), does this classpath overwrite the system classpath from DOS.
Another thing I don't understand. Isn't it wise to use the '.' as a kind of default classpath? My understanding is that this always considers the current directory as a classpath. If this is the case, why do we have to specify exact paths too?
Thanks for the very helpful response, by the way.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

The answer to the IDE question is totally dependant on the IDE.

Visual Cafe used to stomp your System CLASSPATH, which has in recent years (thankfully - are you listening Oracle and Rational?) become unpopular.

JBuilder (4,5 and 6 anyways) does absolutely nothing to your System CLASSPATH, so adding something to JBuilder will not affect other things on your PC. It doesn't change the classpath on your system, but rather supplies a -classpath parameter to both javac and java.

When I first started with java and was trying to get around the whole path/classpath thing, I found more often than not, having absolutely NO environment entry for CLASSPATH helped me a lot. So unless my memory totally fails, both javac and java will assume '.' is in your CLASSPATH without further prompting from you.

You would still have to specify extra paths, if for example, you had a library of jar files somewhere else on your PC. My own system, I have d:\jars that contains a copy of almost every jar in my system. That way if I'm ever using a text-based editor (Textpad) and I need to add things to my classpath, I can just type "d:\jars\xxxx.jar" and I know it will find it there.
Paul Keohan
Ranch Hand

Joined: Mar 15, 2000
Posts: 411
There's a piece of code built into the java Toolkit class that checks for any accessibility classes everytime an AWT window appears. If there is something in the accessibility.properties file it will invoke it. Why is it that I have a class in here, which it is unable to see even though I have my classpath set to the directory that my .class file is in? If I put my [I].class[/I/] file into the ext directory of the jdk, it will be seen. The only answer I can come up with, is that the classpath is being changed as my application starts.
Paul
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting classpath