wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Setting CLASSPATH in XP 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 in XP" Watch "Setting CLASSPATH in XP" New topic


Anirudh Shivaswamy

Joined: Nov 27, 2005
Posts: 5
I would like to compile java programs from any directory. I have set CLASSPATH as mentioned in the beginners' FAQ but I am not able to run javac from any directory. What should I do?
Svend Rost
Ranch Hand

Joined: Oct 23, 2002
Posts: 904
Did you enter it under:
Control Panel -> System -> Environment Variables ?

Did you try to reboot?

/Svend Rost
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074

You should add the bin directory of the JDK in your PATH (not CLASSPATH). Read the JDK installation instructions and the links in my signature.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Anirudh Shivaswamy

Joined: Nov 27, 2005
Posts: 5
I can now compile from the command line in desired directory. But when it comes to running the program(i.e. java dog for example) I get following error message

Exception in thread "main" java.lang.NoClassDefFoundError: Dog

What should I do?
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 41188
If you always want to have the current directory in your classpath, then the CLASSPATH variable (in the plave Svend mentions) should include "." (the dot), which indicates the current directory.

Ping & DNS - my free Android networking tools app
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
If you have been told to set your CLASSPATH, you have been ill advised, which is why you are likely to encounter issues. You should not be setting your CLASSPATH at all.
Try this:


Tony Morris
Java Q&A (FAQ, Trivia)
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
HERE is a page I put together on this topic. See if it helps.

I don't set the PATH or CLASSPATH for Java programs in the control panel, but as part of a batch file for each program or command window session. I keep several JREs and JDKs on my system and I can set these variables for just the environment I need for each program. For example, my work stuff has to run under 1.3 and my fun stuff runs under 1.5.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
[Stan]: I don't set the PATH or CLASSPATH for Java programs in the control panel, but as part of a batch file for each program or command window session.

Good approach.

For people wishing to follow Tony's advice (which is also good, but incomplete), you would probably benefit from the following tip: instead of setting the CLASSPATH, you can run java or javac using the -classpath or -cp options. This is essentially equivalent to setting the CLASSPATH variable, except it's only set for that one particular invocation of the javac or java command. Which is usually a good thing. The problem with setting the CLASSPATH via the control panel (or in a loging script) is that there's only one CLASSPATH on the machine (or rather, one per user, maybe), and when you change it, you're changing it for all programs that use it. Which has a good chance of breaking them -- especially if you're a beginner.

Other common alternatives to setting CLASSPATH or using the -cp options are:
  • let an IDE take care of it for you (the IDE may well use -cp behing the scenes)
  • use java's -jar option and make sure all necessary classes are in the jar
  • use a war or ear file and let a web server or app server take care of finding the classes
  • create a custom class loader to find classes by some other method

  • Since this is the Beginner forum, ignore those last three suggestions. I recommend using -cp, or setting CLASSPATH in a batch file as Stan suggests (so it's valid only for the current session), or using an IDE. Well, the -jar option is not bad either, but creating the jar is a little bit more advanced.
    [ December 02, 2005: Message edited by: Jim Yingst ]

    "I'm not back." - Bill Harding, Twister
    Anirudh Shivaswamy

    Joined: Nov 27, 2005
    Posts: 5
    I solved the problem guys!Thanks to Stan James. All i did was type the following before compiling \ running:

    set classpath=.

    Now only if I could figure out why it worked. And how to automate it so that I need not type it each time. Don't I sound lazy?
    prasad krishnan

    Joined: Dec 03, 2005
    Posts: 5
    I too is very lazy in typing commands in the prompt again and again. what i do is make a file with .bat say lazy.batas extension with all the commands you want to execute and in the command prompt go to the place where lazy.bat sits and just hit lazy.bat....its done.
    Jesper de Jong
    Java Cowboy
    Saloon Keeper

    Joined: Aug 16, 2005
    Posts: 14074

    set classpath=.

    First, why this worked: You're setting the classpath to the current directory (".") here, so by doing this, Java will look for classes in the directory that you're running the command from.

    To avoid having to type this every time: Make sure that you do not have a CLASSPATH environment variable set at all. If it's not set, Java will by default use the current directory.

    Try this (Windows XP): Click on Start, move your mouse cursor to My Computer, right-click on it and choose Properties in the popup menu. Now go to the Advanced tab and click the button Environment Variables in the bottom of the dialog box. Make sure that there is no CLASSPATH variable in either the User Variables or System Variables list (if there is, click it and press the Delete button). Click OK to close the dialog boxes.

    Open a new command prompt and type: echo %classpath%

    It should respond with: %classpath%
    which means the classpath environment variable is not set.
    I agree. Here's the link: http://aspose.com/file-tools
    subject: Setting CLASSPATH in XP
    Similar Threads
    How to run Java programs made in older versions
    CLASSPATH driving me crazy
    NoClassDefFoundError (HELP)
    a class in default invoked by a class in another package
    Compiling Question