aspose file tools*
The moose likes Beginning Java and the fly likes How to set up Java Classpath? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to set up Java Classpath?" Watch "How to set up Java Classpath?" New topic
Author

How to set up Java Classpath?

Haizea O'Brien
Greenhorn

Joined: Nov 06, 2008
Posts: 4
Hi. I'm not virtually a newbie in Java, (I've been programming using Java for almost half a year from now) but I still struggle with this classpath thing. I can program both in a text editor and IDE and so far, I don't have any problems in compiling not until I started creating packages and tried to compile them using the command line. I've went to Java's official site and searched for "Setting Java Classpath" (http://java.sun.com/j2se/1.3/docs/tooldocs/win32/classpath.html), but I still didn't get it. Can somebody please teach me and explain to me how to set up Java Classpath in command line. My OS is Windows XP.


Thank you very much and more power!


[ non sum qualis eram... ]
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
From command line, when you compile or run ie you use javac or java you can use the -cp or -classpath options.
example :

or

try it and let us know.


Cheers!
RSR
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
And Welcome to the Ranch.
Haizea O'Brien
Greenhorn

Joined: Nov 06, 2008
Posts: 4
Thanks for the warm welcome and for the speedy reply. I tried what you said and unfortunately, it didn't work.
rakesh sugirtharaj
Ranch Hand

Joined: Dec 16, 2007
Posts: 151
What is the error/message you are getting? Are you trying to compile or run or both?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
When you have lots of classes, it might be easier to use a tool like "ant" to record all the class names. In fact NetBeans does that automatically.

If you have one class it startsand there are three ways to compile it.
  • Put Bar in the foo folder, navigate inside the folder foo and write java Bar.java
  • Put Bar in the foo folder, navigate to the foo folder's enclosing directory and write javac foo/Bar
  • Put Bar in a folder, navigate to that folder and write javac -d . Bar.java. This last method will create a foo folder and put Bar.class inside that folder.
  • In the situation you describe, you don't do anything with the classpath.

    If the class FooFoo has not already been compiled, the compiler won't compile it. So you would have to compile any files which are dependencies for the present class. Either compile them first, or write several names after javac:

    javac -d . FooFoo.java Bar.java

    That should sort out your problem.
    Amit Ghorpade
    Bartender

    Joined: Jun 06, 2007
    Posts: 2716
        
        6

    Originally posted by Haizea O'Brien:
    I tried what you said and unfortunately, it didn't work.


    You need to elaborate on that "it". So that people better understand your problem. Read this for more details.

    Do you know how to set environment variable in Windows XP ?.
    You can use that too, depending upon whether you want the classpath for individual files/programs or all of them
    [ November 07, 2008: Message edited by: Amit Ghorpade ]

    SCJP, SCWCD.
    |Asking Good Questions|
    arulk pillai
    Author
    Ranch Hand

    Joined: May 31, 2007
    Posts: 3223
    Say, you have a class named �Pet� in a project folder �c:\myProject� and package named com.xyz.client, will you be able to compile and run it as it is?




    To run --> c:\myProject> java com.xyz.client.Pet

    The answer is no and you will get the following exception: �Exception in thread "main" java.lang.-NoClassDefFoundError: com/xyz/client/Pet�. You need to set the classpath. How can you do that? One of the following ways:


    1.Set the operating system CLASSPATH environment variable to have the project folder �c:\myProject�. [Shown in the above diagram as the System �classpath class loader]
    2.Set the operating system CLASSPATH environment variable to have a jar file �c:/myProject/client.jar�, which has the Pet.class file in it. [Shown in the above diagram as the System �classpath class loader].
    3.Run it with �cp or �classpath option as shown below:



    OR




    Also make sure that your JAVA_HOME system property is correctly setup. Type c:\set command on windows to check this.

    and also your path is correctly set. You can check this with

    c:\java -version

    The above command should display the version of the Java you are running.


    Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39396
        
      28
    Arulk, surely you don't set the system CLASSPATH, beyond checking it has the usual . in? Which it must have because the poster says their older code worked all right.
    You just need to navigate to the folder where the source files are.
    It is sometimes easier to have two command line windows open, one inside the named package folder, the other one level "up" (towards root) because it saves writing lots of "cd" instructions.

    You only need to set a CLASSPATH when you are using code in another jar outside your present folder. And then you use the -cp options in the command line (or even in the manifest file in your jar).
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39396
        
      28
    Imagine you have 3 files Foo.java, Baa.java, and Baaaaaar.java. These three classes depend on each other and are declared with different package names. Imagine Foo has the main method. Imagine Bar has a Baaaaaaar field. Imagine I could spell Baaaaaaar the same every time.

    Option 1:
  • Put all the 3 files in a folder, say C:\Campbell\java.
  • Navigate to C:\Campbell\java
  • Compile with javac -d . Foo.java Baa.java Baaaaaar.java
  • Use Windows Explorer to see the folder structure; there are folders for all the packages now.
  • Option 2:
  • Create folders the same names as the packages.
  • Put each .java file in its own package folder
  • Compile with javac foo\Foo.java baa\Baa.java baaaaar\Baaaaaar.java
  • Option 3:
  • Create folders the same names as the packages.
  • Put each .java file in its own package folder
  • Navigate to folder baaaaar.
  • Compile with javac Baaaaar.java
  • Navigate to folder bar
  • Compile with javac Bar.java
  • Navigate to folder foo
  • Compile with javac Foo.java
  • Note you have to compile the "supplier" classes first and the "client" classes afterwards.

    To execute the files: navigate to C:\Campbell\java and write java foo.Foo
    felicia adedotun
    Greenhorn

    Joined: Oct 31, 2006
    Posts: 21
    @poster, am having same problem too and same operating system


    i can only run java but can't run javac

    you know, it is so frustrating.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39396
        
      28
    Originally posted by felicia adedotun:
    @poster, am having same problem too and same operating system


    i can only run java but can't run javac

    you know, it is so frustrating.
    No, that's a different and more basic problem. And why didn't you say so earlier? The "can't run javac" problem is probably the first one discussed here. If that doesn't help, please return to your original thread and post the actual error message.
    Haizea O'Brien
    Greenhorn

    Joined: Nov 06, 2008
    Posts: 4
    To everybody who had spent their time and effort in posting and answering my question, thank you very much, specially to rakesh sugirtharaj, Campbell Ritchie, Amit Ghorpade, arulk pillai and felicia adedotun. Now, I understand classpath better. Again, thank you and more power.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39396
        
      28
    Well done, and we are only too pleased to be able to help.
    Gamini Sirisena
    Ranch Hand

    Joined: Aug 05, 2008
    Posts: 347
    Expanding a bit on Campbell's option1..

    You can also do javac -d . *.java

    And I guess this is a typo..

    Originally posted by Campbell Ritchie:
    and there are three ways to compile it.

    Put Bar in the foo folder, navigate inside the folder foo and write java Bar.java



    You mean, write javac Bar.java and not java Bar.java, right?
    [ November 22, 2008: Message edited by: Gamini Sirisena ]
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39396
        
      28
    Originally posted by Gamini Sirisena:
    Expanding a bit on Campbell's option1..

    You can also do javac -d . *.java

    And I guess this is a typo..




    You mean, write javac Bar.java and not java Bar.java, right?

    [ November 22, 2008: Message edited by: Gamini Sirisena ]


    Yes, you are right, it is a mistake. Sorry.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to set up Java Classpath?