Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java setting the classpath on windows xp

Options
  • 29-09-2010 9:19pm
    #1
    Registered Users Posts: 1,552 ✭✭✭


    I want to be able to run the java .class files from any directory.
    I have the path variable in system set to C:\Program Files\Java\jdk1.6.0_21\bin
    I have the classpath variable set to C:\Program Files\Java\jdk1.6.0_21\lib.

    I can compile programs using javac via the command prompt fine.
    The problem occurs when I try running the program using java via the command prompt, I get java.lang.NoClassDefFoundError: Hello
    So it can't find the class file of the program I'm running.
    How can I set the classpath so that when I run programs via the command line their class file will be found no problem wherever they are located?


Comments

  • Registered Users Posts: 1,998 ✭✭✭lynchie


    quinnd6 wrote: »
    I want to be able to run the java .class files from any directory.
    I have the path variable in system set to C:\Program Files\Java\jdk1.6.0_21\bin
    I have the classpath variable set to C:\Program Files\Java\jdk1.6.0_21\lib.

    I can compile programs using javac via the command prompt fine.
    The problem occurs when I try running the program using java via the command prompt, I get java.lang.NoClassDefFoundError: Hello
    So it can't find the class file of the program I'm running.
    How can I set the classpath so that when I run programs via the command line their class file will be found no problem wherever they are located?

    Add the jar or the directory your classes are in to the classpath you set above.


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    OK I can compile/run classes from any directory. Add something like this to the "Path" environment variable...

    ;C:\Program Files\Java\jdk1.6.0_20\bin;

    Works for me anyhow...


  • Registered Users Posts: 1,552 ✭✭✭quinnd6


    I did what you guys said and now I can only compile and run programs in the directory that I added to the classpath variable.
    I cannot run java programs in any other directory but can only compile them and if I try running them in another directory I get the java.lang.NoClassDefFoundError

    My classpath user variable is C:\Program Files\Java\jdk1.6.0_21\lib\tools.jar;c:\Java Programs
    My path system variable is ;C:\Program Files\Java\jdk1.6.0_21\bin

    Is there anything else I need to have in these variables?


  • Registered Users Posts: 498 ✭✭bobbytables


    quinnd6 wrote: »
    How can I set the classpath so that when I run programs via the command line their class file will be found no problem wherever they are located?
    Ah I think I see what you're doing wrong. Typically when you compile native apps, you end up with an executable for that platform. E.g. java.exe is a native app. Now in your case you are trying to launch the Java VM and pass a class with a public static void main(String args[]) function contained within. Except you are treating the .class (byte code) file in conjunction with the java.exe file as something that would exist on the system path and classpath. Java.exe is on the system path, but the class path typically refers to the current directory or a series of .jar files.

    An option for your to run this from any directory is to unify the two in a batch file, that contains
    @echo off
    java c:\path\to\Hello
    
    You can call that batch file hello.bat for simplicity sake. Add that batch file to a location on the system path and voilla you will be able to run the program by typing "hello" from any directory.


  • Registered Users Posts: 1,552 ✭✭✭quinnd6


    You see what happened was my computer had some spyware and I used adaware in safe mode which rendered my pc inoperable so I had to reinstall windows so everything got wiped off the drive and so I've had to reinstall everything included my java.
    I remember the way I had it set up before I could compile and run programs no matter where they were stored on my pc.
    I would just use the command line, go the directory where I had the .java file stored, compile it using javac programname.java
    and run it using java programname
    I didn't have to mess around with batch files or any of that lark it was just that straightforward.
    Now I can compile ok wherever the java file is but it just can't find the freaking program when I try running it using java programname.
    You see I want to run the program from the directory it's in.
    I probably didn't explain what I wanted to do very well which was just be able to run programs without that error popping up and be able to go to the directory where the program is and run it and have a program in another directory and go to that directory and run it without getting that NoClassDefFoundError.


    What I will do is just put all my programs into that java programs directory and that way they will run and compile properly.
    It's just unnecessary hassle creating batch files.
    Thanks for answering though.


  • Advertisement
  • Registered Users Posts: 54 ✭✭ejvilla


    I might not be understanding what you need, so let me tell you my assumptions first...

    You have a folder, something like C:\JavaProject.
    In that folder you have a java file Hello.java

    You opened a command prompt.
    You entered the JavaProject directory (by typing "cd C:\JavaProject").
    When you type 'dir', you can see the filed Hello.JAVA.
    You typed 'javac Hello.java'
    This completed successfully and when you type 'dir' you see that a new file has been created called Hello.CLASS.
    You type 'java Hello'
    The message "java.lang.NoClassDefFoundError: Hello" is printed on the command prompt.

    Have you done anything differently?

    If you were able to compile the java file with 'javac Hello.java' (and it compiled successfully) then you will be able to run it with 'java Hello'. NoClassDefFound means it can't find the compiled class.

    You are either in the wrong directory or your compilation failed.

    The fact that you are seeing 'NoClassDefFound' means you can "run java [...] from any directory". To be absolutely sure about this, 'cd' to the directory you want and just type 'java'. You will see usage information.


  • Registered Users Posts: 498 ✭✭bobbytables


    ejvilla wrote: »
    The fact that you are seeing 'NoClassDefFound' means you can "run java [...] from any directory". To be absolutely sure about this, 'cd' to the directory you want and just type 'java'. You will see usage information.
    Exactly!. If you are getting a class def not found message then java.exe is successfully being found on your system path, so you can rule that out as being your problem.

    Is the current directory part of your classpath environment var?. If not stick ;. at the end of it, and try again.


  • Registered Users Posts: 1,552 ✭✭✭quinnd6


    I was trying to run the programs on my desktop folder.
    I can only run programs on the java programs folder which is what I've added to the classpath.
    I went to the desktop folder.
    The java command works and I can compile programs that are there.
    When I try running the program I get noclassdeffounderror.
    It appears when I use the java command it will only find class files if they are in the java programs folder.
    Even if I go to my desktop folder and run the java command it seems to search for the program in the java programs folder not the folder I'm actually in on the command line.
    I know this cos I compiled a Hello program in the java programs folder.
    Then I went to the desktop folder and typed in java Hello.
    The program ran fine.
    I then deleted the Hello.class file in the java programs folder.
    I then tried to run java Hello in my desktop folder and it came up with noclassdeffounderror.


  • Registered Users Posts: 2,762 ✭✭✭Sheeps


    learn how to use the java switches correctly.


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    I think you missed bobbytables answer, it will let you run java your classes from your current directory.

    Add ";." to the end of the classpath as bobbytables suggests. It tells the JVM to search the current directory for java classes
    Exactly!. If you are getting a class def not found message then java.exe is successfully being found on your system path, so you can rule that out as being your problem.

    Is the current directory part of your classpath environment var?. If not stick ;. at the end of it, and try again.
    quinnd6 wrote: »
    I was trying to run the programs on my desktop folder.
    I can only run programs on the java programs folder which is what I've added to the classpath.
    I went to the desktop folder.
    The java command works and I can compile programs that are there.
    When I try running the program I get noclassdeffounderror.
    It appears when I use the java command it will only find class files if they are in the java programs folder.
    Even if I go to my desktop folder and run the java command it seems to search for the program in the java programs folder not the folder I'm actually in on the command line.
    I know this cos I compiled a Hello program in the java programs folder.
    Then I went to the desktop folder and typed in java Hello.
    The program ran fine.
    I then deleted the Hello.class file in the java programs folder.
    I then tried to run java Hello in my desktop folder and it came up with noclassdeffounderror.


  • Advertisement
  • Registered Users Posts: 1,552 ✭✭✭quinnd6


    That worked brilliant thanks.


Advertisement