Ok, so. I ordered a book on Java (Sams teach yourself java in 21 days) a week ago, and it came in just yesterday. I am working on the first example code, and I keep getting this error when I try to compile the main code:
C:\VolcanoApplication.java:5: error: cannot find symbol
VolcanoRobot dante = new VolcanoRobot();
^
symbol: class VolcanoRobot
location: class VolcanoApplication
C:\VolcanoApplication.java:5: error: cannot find symbol
VolcanoRobot dante = new VolcanoRobot();
^
symbol: class VolcanoRobot
location: class VolcanoApplication
And the main code Im trying to compile is:
public class VolcanoApplication
{
public static void main(String[] arguments)
{
VolcanoRobot dante = new VolcanoRobot();
dante.status = "exploring";
dante.speed = 2;
dante.temperature = 510;
dante.showAttributes();
System.out.println("Increasting speed to 3.");
dante.speed = 3;
dante.showAttributes();
System.out.println("Changing temperature to 670.");
dante.temperature = 670;
dante.showAttributes();
System.out.println("Checking the temperature.");
dante.checkTemperature();
dante.showAttributes();
}
}
and the VolcanoRobot.java file:
public class VolcanoRobot
{
String status;
int speed;
float temperature;
void checkTemperature()
{
if(temperature > 660)
{
status = "returning home";
speed = 5;
}
}
void showAttributes()
{
System.out.println("Status: " + status);
System.out.println("Speed: " + speed);
System.out.println("Temperature: " + temperature);
}
}
I am unable to get javac to run anywhere in command prompt (I'm running xp) so I navigate to where my javac.exe is (C:\Program Files\Java\jdk1.7.0_03\bin) and compile VolcanoApplication from there (VolcanoApplication is found on the root of C:)
When I just type Java anywhere I get the menu, but not javac. I declared the path and classpath variables, and yet it doesn't work. any suggestions?
Your best bet is to make javac
work from any directory by going into the environment variables and changing your PATH
so it includes C:\Program Files\jdk1.7.0_03\bin
.
Once you've done that, in a command prompt typing javac
anywhere should work.
The reason javac
isn't finding the VolcanoRobot.java
file is that it's not in the path that javac
searches for source files. By default, that path includes the current directory, so if you cd
to the directory containing VolcanoApplication.java
and VolcanoRobot.java
, then
javac VolcanoRobot.java VolcanoApplication.java
...should do it. If it doesn't, add -cp .
:
javac -cp . VolcanoRobot.java VolcanoApplication.java
You should then be able to run it via
java VolcanoApplication
...or
java -cp . VolcanoApplication
Update: Since my main workstation is Linux-based, I hadn't done this under Windows 7 (used to do it all the time with Windows XP) and so I got to wondering whether there was something special about it. Doesn't look like there is. I installed the JDK on my Windows 7 box and didn't have any trouble using it. Here's exactly what I did:
javac
and pressed Enter, just to make sure I didn't have one installed I didn't remember. I got the usual "...is not recognized as an internal or external command" error.Path
.bin
directory, which was at C:\Program Files\Java\jdk1.7.0_03\bin
.;
) (note: not a colon, and with no spaces around it), and then pasted the path from the clipboard.javac
and pressed Enter. I got the javac
help listing.C:\tmp\j
).Typed:
javac VolcanoRobot.java VolcanoApplication.java
...and pressed Enter. I got no errors.
Typed:
java VolcanoApplication
...and pressed Enter. It worked just fine, I got the output I'd expect from looking at the source files.
So there's no problem doing this on Windows 7. Perhaps what I did above will be helpful to you.