简体   繁体   English

我的第一个“Hello World”Java代码非常简单

[英]Very simple problem with my first “Hello World” Java code

I saved this code in FirstApp.java: 我在FirstApp.java中保存了这段代码:

class FirstApp {
    public static void main (String[] args) {
        System.out.println("Hello World");
    }
}

Then this is what I get when trying to compile and run: 这是我在尝试编译和运行时得到的:

$ javac FirstApp.java
$ java FirstApp
Exception in thread "main" java.lang.UnsupportedClassVersionError: FirstApp : 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)
Could not find the main class: FirstApp. Program will exit.

What I am doing wrong? 我做错了什么?

You may have multiple versions of the JDK installed. 您可能安装了多个版本的JDK。 See: http://www.java.net/node/664117 请参阅: http//www.java.net/node/664117

It means that you compiled your classes under a specific JDK, but then try to run them under older version of JDK. 这意味着您在特定的JDK下编译了类,但是尝试在旧版本的JDK下运行它们。

Basically it means you compiled with a newer version of Java and are trying to run with an older version, such as compiling with Java 7 but trying to run in a Java 6 environment. 基本上它意味着您使用较新版本的Java编译并尝试使用旧版本运行,例如使用Java 7进行编译但尝试在Java 6环境中运行。

You have 3 options. 你有3个选择。

1) Upgrade your runtime environment to match your development environment. 1)升级运行时环境以匹配您的开发环境。 (Make your JRE match your JDK.) (使您的JRE与您的JDK匹配。)

2) Downgrade your dev environment to match your runtime. 2)降级您的开发环境以匹配您的运行时。 (Make your JDK match your JRE.) (使您的JDK与您的JRE匹配。)

3) Use the -source and -target target args when compiling. 3)编译时使用-source和-target目标args。 So, for instance, if your runtime is 1.6, and your JDK is 7, you'd do something like javac -source 1.6 -target 1.6 *.java (Double check the docs for details, I might not have it quite right.) 因此,例如,如果您的运行时为1.6,而您的JDK为7,那么您将执行类似javac -source 1.6 -target 1.6 * .java的操作(请仔细检查文档以获取详细信息,我可能没有这么做。)

This error happens when you run with a different runtime than you compiled with. 使用与编译时不同的运行时运行时会发生此错误。 Check your paths and make sure you are compiling and running with the same version. 检查路径并确保使用相同版本编译和运行。

more about this error here: http://geekexplains.blogspot.com/2009/01/javalangunsupportedclassversionerror.html 有关此错误的更多信息,请访问: http//geekexplains.blogspot.com/2009/01/javalangunsupportedclassversionerror.html

Check your compiler path when you are trying to run that program. 在尝试运行该程序时,请检查编译器路径 Make sure the path is same when compiling and running. 编译和运行时确保路径相同。

This occurs when you attempt to run a java program with a lower level JVM than what it was compiled with. 当您尝试使用比编译时更低级别的JVM运行Java程序时,会发生这种情况。 Somehow, your path is setup in such a way that javac is a higher version level than java . 不知何故,您的路径设置方式使得javac的版本级别高于java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM