• 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

Unable to set CLASSPATH in Linux-based OS

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I use the following code for MyClass.java



I create the directory structure as demonstrated in Sierra/Bates, Chapter 10. However, I didn't create the subdirectories of myProject/classes.
Please see screenshot (directory_structure.png)

I then navigage to /myProjects directory and type in the following

javac -d classes source/com/wickedlysmart/MyClass.java

It compiles, and when I navigate to myProject/classes, I notice that the class file is located in

myProject/classes/com/wickedlysmart/MyClass.java

Now, I read ahead in Chapter 10, and it discusses settings the classpath. This is my problem!

I navigage to myProject and type in the following:

-classpath ../classes/com/wickedlysmart

I get the error:

bash: -classpath: command not found


I am using Linux based OS (Ubuntu 9.04). Please assist.
directory_structure.png
[Thumbnail for directory_structure.png]
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I assume you want to set an environment variable called CLASSPATH. On Linux using the bash shell:To see that you set it right, type:
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:
I navigage to myProject and type in the following:

-classpath ../classes/com/wickedlysmart

I get the error:

bash: -classpath: command not found



That's not how you set the classpath environment variable. That, when used with the java and javac commands, is how you add the switch to ask those commands to use a specific classpath.

Henry
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was finally able to set the classpath. Now when type in:

echo $CLASSPATH

I get:

../classes/com/wickedlysmart


I have a follow-on question:

Created the program /myProject/Another.java that uses MyClass.class, which is located at ../classes/com/wickedlysmart. Below is the code for Another.java, from Sierra/Bates, Cha 10:




I type in the following compile statement in myProject directory:

javac Another.java

And, I get SIX errors!

Another.java:1: package com.foo does not exist
import com.foo.MyClass; // either import will work
^
Another.java:2: package com.foo does not exist
import com.foo.*;
^
Another.java:6: cannot find symbol
symbol : class MyClass
location: class Another
MyClass m1 = new MyClass(); // alias name
^
Another.java:6: cannot find symbol
symbol : class MyClass
location: class Another
MyClass m1 = new MyClass(); // alias name
^
Another.java:7: package com.foo does not exist
com.foo.MyClass m2 = new com.foo.MyClass(); // full name
^
Another.java:7: package com.foo does not exist
com.foo.MyClass m2 = new com.foo.MyClass(); // full name
^
6 errors



Please advise.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your CLASSPATH environment variable holds a relative path so it's important that you run "javac Another.java" from the correct directory. From which directory did you run it?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Henry said, don't use relative path in your classpath. If Another.java is in the myProject directory, you can compile it by navigating to myProject directory from shell prompt and issue the command
javac -classpath .:classes Another.java

This command should compile Another.java successfully. The thing to note here is that we include classes directory into the classpath not classes/com/wickedlysmart as com/wickedlysmart is a part of the package name of MyClass...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tom Reilly wrote:Your CLASSPATH environment variable holds a relative path so it's important that you run "javac Another.java" from the correct directory. From which directory did you run it?




Another.java is located in myProject directory, and that is where I ran it from. I previously created a classpath to relative directory from myProject. Now I went to root and created an absolute path. Please see below terminal dialogue:

Here, I go to ROOT
rsaska@dell-desktop:~/Documents/JavaExamPrep/myProject$ cd /.

Then I set the absolute CLASSPATH
rsaska@dell-desktop:/$ export CLASSPATH=../home/rsaska/Documents/JavaExamPrep/myProject/classes/com/wickedlysmart

I change directories to where Another.java is located
rsaska@dell-desktop:/$ cd /home/rsaska/Documents/JavaExamPrep/myProject

I compile Another.java
rsaska@dell-desktop:~/Documents/JavaExamPrep/myProject$ javac Another.java

Below are the errors I receive:
Another.java:1: package com.foo does not exist
import com.foo.MyClass; // either import will work
^
Another.java:2: package com.foo does not exist
import com.foo.*;
^
Another.java:6: cannot find symbol
symbol : class MyClass
location: class Another
MyClass m1 = new MyClass(); // alias name
^
Another.java:6: cannot find symbol
symbol : class MyClass
location: class Another
MyClass m1 = new MyClass(); // alias name
^
Another.java:7: package com.foo does not exist
com.foo.MyClass m2 = new com.foo.MyClass(); // full name
^
Another.java:7: package com.foo does not exist
com.foo.MyClass m2 = new com.foo.MyClass(); // full name
^
6 errors



 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:As Henry said, don't use relative path in your classpath. If Another.java is in the myProject directory, you can compile it by navigating to myProject directory from shell prompt and issue the command
javac -classpath .:classes Another.java

This command should compile Another.java successfully. The thing to note here is that we include classes directory into the classpath not classes/com/wickedlysmart as com/wickedlysmart is a part of the package name of MyClass...



By the way, I also tried the following dialogue:

rsaska@dell-desktop:~/Documents/JavaExamPrep/myProject$ javac -classpath .:classes Another.java

And get same errors:
Another.java:1: package com.foo does not exist
import com.foo.MyClass; // either import will work
^
Another.java:2: package com.foo does not exist
import com.foo.*;
^
Another.java:6: cannot find symbol
symbol : class MyClass
location: class Another
MyClass m1 = new MyClass(); // alias name
^
Another.java:6: cannot find symbol
symbol : class MyClass
location: class Another
MyClass m1 = new MyClass(); // alias name
^
Another.java:7: package com.foo does not exist
com.foo.MyClass m2 = new com.foo.MyClass(); // full name
^
Another.java:7: package com.foo does not exist
com.foo.MyClass m2 = new com.foo.MyClass(); // full name
^
6 errors
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sandra Bachan wrote:I was finally able to set the classpath. Now when type in:

echo $CLASSPATH

I get:

../classes/com/wickedlysmart



The purpose of the classpath is to specify the root directories to search. And not the exact directory to search.

If the classes are in the com.wickedlysmart package, then the root direction should be "../classes". The compiler and JVM will add the other directories, as it searches for classes in those packages.

Henry
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really embarrassed

I looked at Another.java, and I imported the wrong class. Here is the corrected version:



Now it compiles.

Thank you all for your guidance, and PATIENCE
 
There is no beard big enough to make me comfortable enough with my masculinity to wear pink. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic