0

我是linux的新用户。我刚刚安装了 jdk1.7.0_02.rpm 并尝试使用javac tester.java当我尝试运行文件时编译我的程序时java tester出现这些错误

Exception in thread "main" java.lang.UnsupportedClassVersionError: tester : Unsupported major.minor version 51.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:634)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:277)
at java.net.URLClassLoader.access$000(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:212)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)

找不到主类:tester。程序将会退出。

然后我检查了java -version它是:

java version "1.6.0_22"
OpenJDK Runtime Environment (IcedTea6 1.10.4) (fedora-61.1.10.4.fc16-i386)
OpenJDK Server VM (build 20.0-b11, mixed mode)

现在如何更改此版本?我什至安装了 jre1.7.0_02 但没有任何改变!

这就是正在发生的事情:

在此处输入图像描述

4

5 回答 5

4

在终端中运行:

sudo update-alternatives --config java

并选择首选的 Java 版本。如果您没有看到您正在寻找的版本作为选项,请查看站点。

于 2012-01-27T06:04:48.440 回答
2

Use the cross-compilation options of javac. Specify a -source and -target version and follow the prompts re other options.


Then please tell how?

javac -source 1.6 -target 1.6 -bootclasspath /path/to/1.6/j2se/rt.jar *.java

If you specify source/target, the 1.7 compiler will warn to use the -bootclasspath option, which is important to verify that the classes, methods and attributes referenced in the source are available in the target Jar. Earlier versions of the compiler failed to warn about the bootclasspath.

于 2012-01-27T06:31:09.460 回答
0

好的,首先,您不应该以 root 身份运行 Java 应用程序构建。

Change to a user account with sudo capability, and don't use root privs until you absolutely need them.

It looks like javac has been updated to version 7 but java has not.

What does this return:

javac -version

You should also tell us the current value of your JAVA_HOME environment variable. Like this:

set | grep ^JA
于 2012-01-27T06:07:23.720 回答
0

您需要您的系统在运行 Java 程序时使用 JRE 1.7。为此,请确保将 JAVA_HOME 正确设置为 JRE 1.7:

export JAVA_HOME=/path/to/JRE1.7

还要确保您的 PATH-Variable 指向正确的 java/bin 目录:

export PATH=$PATH:/path/to/JRE1.7/bin

在重新启动/注销之后保留这些环境变量取决于您使用的 Linux 发行版...

编辑:澄清:如果您使用 1.7 版进行编译(默认情况下以 1.7 为目标),则必须使用 >= 1.7 JRE。

所以你有2个选择:

  1. 使用 JRE 1.7 运行您使用 1.7 编译的程序(如上所述)
  2. 编译时使用 javac 的“-target 1.6”cmdline-argument 创建符合 1.6 的代码
于 2012-01-27T07:09:00.687 回答
0

完全没有理由,系统环境中的JAVA_HOME也必须设置。虽然如果您在应用程序属性中设置 JAVA_HOME。

于 2013-09-06T08:52:16.410 回答