Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting CLASSPATH in XP

 
Anirudh Shivaswamy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you enter it under:
Control Panel -> System -> Environment Variables ?

Did you try to reboot?

/Svend Rost
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15283
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anirudh Shivaswamy
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

http://jqa.tmorris.net/GetQAndA.action?qids=25&showAnswers=true
http://jqa.tmorris.net/GetQAndA.action?qids=24&showAnswers=true
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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 ]
     
    Anirudh Shivaswamy
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 15283
    39
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic