Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to set up Java Classpath?

 
Haizea O'Brien
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rakesh sugirtharaj
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch.
 
Haizea O'Brien
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the error/message you are getting? Are you trying to compile or run or both?
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 2854
    10
    Fedora Firefox Browser Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 ]
     
    arulk pillai
    Author
    Ranch Hand
    Posts: 3387
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    Campbell Ritchie
    Sheriff
    Posts: 48635
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 48635
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 21
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @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
    Posts: 48635
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 48635
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well done, and we are only too pleased to be able to help.
     
    Gamini Sirisena
    Ranch Hand
    Posts: 378
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 48635
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic