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
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.
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
posted
0
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?
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...
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
posted
0
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...
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
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
posted
0
I am really embarrassed
I looked at Another.java, and I imported the wrong class. Here is the corrected version: