Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to set CLASSPATH in Linux-based OS

 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • 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]
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • 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:
 
Henry Wong
author
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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?
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Marshal
Pie
Posts: 21504
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic