aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unable to set CLASSPATH in Linux-based OS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unable to set CLASSPATH in Linux-based OS" Watch "Unable to set CLASSPATH in Linux-based OS" New topic
Author

Unable to set CLASSPATH in Linux-based OS

Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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.


[Thumbnail for directory_structure.png]


Marriage Made in Heaven
http://www.youtube.com/user/RohitWaliaWedsSonia
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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

Joined: Jun 01, 2010
Posts: 618
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sandra Bachan
Ranch Hand

Joined: Feb 18, 2010
Posts: 434
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

Joined: Feb 18, 2010
Posts: 434
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
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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

Joined: Feb 18, 2010
Posts: 434
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to set CLASSPATH in Linux-based OS