How to resolve this? I can compile it but I can't run it. I have different versions of javac
and java
. I'm trying to run my .java
file on terminal. But it works in eclipse. I know that version affects this. How to upgrade javac
? Will it affect my eclipse if I do so? Thanks. ;)
[clemjon@localhost ~]$ javac -version
javac 1.6.0_20
[clemjon@localhost ~]$ java -version
java version "1.6.0_21"
Java(TM) SE Runtime Environment (build 1.6.0_21-b06) Java HotSpot(TM) Server VM (build 17.0-b16, mixed mode)
Check which JRE eclipse is using:
Window->Preferences->Java->JREs
Right click the selected one and Edit
.
The JRE home path should be there. Add that path to your path
environment variable (instead of the one that is already there) and that should do the trick for you.
You can also set which JDK version you want Eclipse to compile to:
Window->Preferences->Java->Compiler
My best advice would be to keep track of all the installed JDK's and JRE's.
Choose the ones you want to use by default and add those to your JAVA_HOME
and Path
environment variables; Then setup your Eclipse to the same values using the steps above.
Then if for a particular project you need to use a specific JRE, right click on that project and then go to BuildPath
-> Libraries
-> JRE Library
-> Edit
and pick you JRE.
Disclaimer: I'm not on my dev machine right now so the names may be approximate. :)
This is not an issue to do with different versions of javac
and java
. The problem here is that you are attempting to run a class file whose fully-qualified name ( com.pl.casestudy.First
) doesn't match the class name you passed to java
( First
). You would get the same error if you had the same versions of javac
and java
.
If you have a class whose name is com.pl.casestudy.First
, Java will look for it in a file with name com/pl/casestudy/First.class
(Windows users feel free to reverse the direction of the slashes.) So you will need to run your First
class from the directory that contains the com
subdirectory. I can see from the shell prompt in your stacktrace that you have a directory named casestudy
, I'll assume that the com
and pl
parent directories also exist. Eclipse has probably created them for you.
Try cd
ing out to the directory that contains com
, compiling using
javac com/pl/casestudy/First.java
and running using
java com.pl.casestudy.First
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.