Compile error
I've got a directory structure as follows :
C:\temp\code\one\two
In the folder named "one", I've got a file called AddFive.java, the code as follows:
package one;
publicclass AddFive{
publicstaticint doAddition(int num){
num = num + 5;
return num;
}
}
That compiles fine.
In the folder named "two", I've got a file called GetAnswer.java, the code as follows:package one.two;
import one.*;
publicclass GetAnswer{
publicstaticvoid main(String[] args){
try{
int ans = AddFive.doAddition(15);
System.out.println("Answer : " + ans);
}catch(Exception ioe){
System.out.println("Error : " + ioe);
}
}
}
When I try compile this file it gives the following error
C:\temp\code\one\two>javac GetAnswer.java
GetAnswer.java:11: cannot find symbol
symbol : variable AddFive
location :class one.two.GetAnswer
int ans = AddFive.doAddition(15);
^
Can anyone help me sort this out ?
[2093 byte] By [
gtommoa] at [2007-10-3 4:18:13]

Check the [url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html]javac documentation[/url], epecially the [url=http://java.sun.com/j2se/1.5.0/docs/tooldocs/windows/javac.html#searching]Searching for Types[/url] section.
Then try with something like C:\temp\code\one\two>javac -classpath C:\temp\code GetAnswer.java
orC:\temp\code>javac -classpath . one\two\GetAnswer.java
As a general rule, once you begin to use packages, you may want to use the destination option with the javac command. For example...
javac -d classes HelloWorld.java
The compiler will create a directory structure which mirrors the package structure of the class HelloWorld and place the subdirectories with the compiled class within the directory 'classes'. The advantage of that is that it tends to clear up these sorts of classpath issues that you are seeing with this message here when you include the classpath option with javac.
So in your case, create a folder called "classes" inside the code directory. And then run the javac command from within the code directory for each of your files. Be sure to set the classpath option to refer to the classes directory.
So your working directory becomes C:\temp\code. Within this directory you run the javac command.
First compile the AddFive class
javac -d classes one\AddFive.java
Then compile the GetAnswer class incorporating the classes the directory on the classpath so that the compiler can find the AddFive class.
javac -d classes -classpath classes one\two\GetAnswer.java
If you navigate through the classes directory you should see the subdirectories one and two with the compiled classes within them.