• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to set up Java Classpath?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And Welcome to the Ranch.
 
Haizea O'Brien
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the error/message you are getting? Are you trying to compile or run or both?
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
     
    Bartender
    Posts: 2856
    10
    Firefox Browser Fedora Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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 ]
     
    Author
    Posts: 3473
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
     
    Greenhorn
    Posts: 21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Well done, and we are only too pleased to be able to help.
     
    Ranch Hand
    Posts: 378
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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
    Marshal
    Posts: 79239
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Honk if you love justice! And honk twice for tiny ads!
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic