JDK problems - Unable to load class files
-
28-05-2021 - |
Frage
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?
Lösung
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:
- Opened a command prompt and typed
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. - Downloaded the JDK installer from Oracle.
- Ran it, letting it install to its default location.
- Opened the Control Panel.
- Typed "environ" into the search box (because I'm lazy and don't bother to keep track of where they've moved it to this week).
- Clicked the "Edit system environment variables" choice and clicked Yes on the admin permissions pop-up question. This opened a "System Properties" dialog with the "Advanced" tab open.
- Clicked the "Environment Variables..." button on that tab.
- In the "System variables" box at the bottom, scrolled down to
Path
. - With that highlighted, clicked the "Edit..." button, which opened the "Edit System Variable" box.
- In Windows Explorer, navigated to the JDK's
bin
directory, which was atC:\Program Files\Java\jdk1.7.0_03\bin
. - Clicked in the address bar, selected all, and copied that path to the clipboard.
- Back in the "Edit System Variable" box, I put the cursor at the end of the path, typed a semicolon (
;
) (note: not a colon, and with no spaces around it), and then pasted the path from the clipboard. - Clicked the OK button on that box, the OK button on the "Environment Variables" box, and the OK button on the "System Properties" box.
- Opened a new commmand prompt.
- Typed
javac
and pressed Enter. I got thejavac
help listing. - Created the two volcano source files and put them in a directory (in my case,
C:\tmp\j
). - Changed into that directory.
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.
Andere Tipps
If the C:\Program Files\Java\jdk1.7.0_03\bin folder is entered into your system path environment variable, you might need to reopen cmd.exe, as it will not load these environment variables on the fly.
You then need to run javac from the root of your application.