Results 1 to 10 of 10
Like Tree2Likes
  • 1 Post By Junky
  • 1 Post By pbrockway2

Thread: How to set the classpath

  1. #1
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default How to set the classpath

    Hi there!

    I posted something earlier about having a hard time with my command prompt. Well, the good news is I fixed the biggest error of them all. However, there is an error with my classpath. I clicked System, advanced settings, and then I clicked Environment Variables. I found this in the classpath:

    .;C:\Program Files\Java\jre6\lib\ext\QTJava.zip

    What is supposed to actually be in the classpath? What is the value?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: How to set the classpath

    When I have done command-line work in the past, I have taken care to set the system path to make the java compiler visible, but usually don't set or change the system classpath. Instead I've set it on the command line itself with the -cp command. Sometimes all it needs is a period . but sometimes it requires more.

  3. #3
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: How to set the classpath

    What is the -cp command and how does it work? Can you explain more? Thanks :)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  5. #5
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: How to set the classpath

    I read that and I don't understand what to put for C:> set CLASSPATH=classpath1;classpath2... What do I put for the classpath option? Do I put the directory to where the .class file is located?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: How to set the classpath

    javac -cp path_to_classes FileName.java
    Fubarable likes this.

  7. #7
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: How to set the classpath

    Okay, so I did that and I still keep getting the error:

    Could not load or find main class MyFirstJavaProgram.java

    What am I doing wrong? What is going on?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: How to set the classpath

    Okay, so I did that
    What did you do? Copy and post the exact command and the resulting output as it appears at the command line.

  9. #9
    BenH is offline Member
    Join Date
    Dec 2011
    Posts
    48
    Rep Power
    0

    Default Re: How to set the classpath

    Okay:

    cd Desktop
    javac -cp path_to_classes MyFirstJavaProgram.java

    java -cp path_to_classes MyFirstJavaProgram.java

    Even if I take out the path_to_classes I still get the error as well.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: How to set the classpath

    Please note that you did not in fact copy and post the output from the commands.

    The previous suggestion was not that you use literally "path_to_classes" in the command. Rather the intent was that you use the actual classpath that you intend for the commands. Supposing that MyFirstJavaProgram.java is a file on your desktop, you might try something like:

    Java Code:
    cd Desktop
    javac -cp . MyFirstJavaProgram.java
    java -cp . MyFirstJavaProgram
    Mind the dot! It stands for "the current directory" and will serve for "path_to_classes" in this simple case. Notice, too, that while the compiler command (javac) expects a file to compile, the runtime (java) expects the name of a class which does not have a dot in it. MyFirstJavaProgram.java is the name of the file, MyFirstJavaProgram is the name of the class.

    If you get stuck do copy and post the output from the command line. The contents of the MyFirstJavaProgram.java file also make a difference (the class being defined should not be declared to be in a package or that changes things, also it must have a well defined main() method.) If you are in any doubt post the contents of that file as well.
    Fubarable likes this.

Similar Threads

  1. Replies: 1
    Last Post: 05-03-2011, 03:28 AM
  2. DTD on a classpath
    By frenk_castle in forum XML
    Replies: 0
    Last Post: 03-29-2011, 08:26 PM
  3. Classpath
    By JavaJunkie in forum New To Java
    Replies: 1
    Last Post: 04-07-2009, 08:17 AM
  4. Classpath
    By dawiz001 in forum New To Java
    Replies: 3
    Last Post: 03-18-2009, 03:18 PM
  5. Classpath on mac osx
    By jacobb in forum JDBC
    Replies: 0
    Last Post: 06-12-2008, 10:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •