[英]I have different versions of javac and java. I'm trying to run my .java file on terminal. But it works in eclipse
How to resolve this? 如何解决呢? I can compile it but I can't run it.
我可以编译它,但不能运行它。 I have different versions of
javac
and java
. 我有不同版本的
javac
和java
。 I'm trying to run my .java
file on terminal. 我正在尝试在终端上运行
.java
文件。 But it works in eclipse. 但是它可以在日食中工作。 I know that version affects this.
我知道该版本会影响到这一点。 How to upgrade
javac
? 如何升级
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) Java™SE运行时环境(内部版本1.6.0_21-b06)Java HotSpot™服务器VM(内部版本17.0-b16,混合模式)
Check which JRE eclipse is using: 检查使用哪个JRE eclipse:
Window->Preferences->Java->JREs
Right click the selected one and Edit
. Window->Preferences->Java->JREs
右键单击选定的一个,然后单击Edit
。
The JRE home path should be there. JRE主目录路径应该在那里。 Add that path to your
path
environment variable (instead of the one that is already there) and that should do the trick for you. 将该路径添加到您的
path
环境变量(而不是已经存在的环境变量)中,这将为您解决问题。
You can also set which JDK version you want Eclipse to compile to: 您还可以设置希望Eclipse编译到的JDK版本:
Window->Preferences->Java->Compiler
My best advice would be to keep track of all the installed JDK's and JRE's. 我最好的建议是跟踪所有已安装的JDK和JRE。
Choose the ones you want to use by default and add those to your JAVA_HOME
and Path
environment variables; 选择默认情况下要使用的变量,并将其添加到
JAVA_HOME
和Path
环境变量中; Then setup your Eclipse to the same values using the steps above. 然后使用上述步骤将Eclipse设置为相同的值。
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. 然后,如果对于特定项目,您需要使用特定的JRE,请右键单击该项目,然后转到
BuildPath
> Libraries
-> JRE Library
-> Edit
并选择您的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
. 这与使用不同版本的
javac
和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
). 这里的问题是,您正在尝试运行其完全限定名称(
com.pl.casestudy.First
)与传递给java
( First
)的类名称不匹配的类文件。 You would get the same error if you had the same versions of javac
and java
. 如果您具有相同版本的
javac
和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. 如果您有一个名称为
com.pl.casestudy.First
的类,则Java会在名称为com/pl/casestudy/First.class
的文件中查找它(Windows用户可以随意反斜杠的方向。)您将需要从包含com
子目录的目录中运行First
类。 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. 我可以从堆栈跟踪中的shell提示中看到,您有一个名为
casestudy
的目录,我假设com
和pl
父目录也存在。 Eclipse has probably created them for you. Eclipse可能已经为您创建了它们。
Try cd
ing out to the directory that contains com
, compiling using 尝试
cd
出包含com
的目录,使用进行编译
javac com/pl/casestudy/First.java
and running using 并使用
java com.pl.casestudy.First
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.