简体   繁体   English

程序未在Eclipse中运行

[英]Program doesn't run in Eclipse

I'm learning Java and one of the examples of the book runs perfectly from command line, but throws an error from Eclipse. 我正在学习Java,本书的示例之一可以从命令行完美运行,但是会从Eclipse中引发错误。 The script is this: 脚本是这样的:

    /**
     * This program displays a greeting from the authors.
     * @version 1.20 2004-02-28
     * @author Cay Horstmann
     */
    public class Welcome
    {
       public static void main(String[] args)
       {
          String[] greeting = new String[3];
          greeting[0] = "Welcome to Core Java";
          greeting[1] = "by Cay Horstmann";
          greeting[2] = "and Gary Cornell";

          for (String g : greeting)
          System.out.println(g);
       }
    }

The error I get in Eclipse is 'Exception in thread "main" java.lang.Error: Unresolved compilation problem: Syntax error, 'for each' statements are only available if source level is 1.5 or greater' The book says I would get an error in that line (the for) if the JDK was too old, but then why does it run well from the command line? 我在Eclipse中遇到的错误是“线程“主”中的异常” java.lang.Error:未解决的编译问题:语法错误,“ for each”语句仅在源级别为1.5或更高时才可用。如果JDK太旧,则该行(for)中的错误,但是为什么它在命令行中运行良好? Thanks 谢谢

The reason is because your Eclipse instance uses a different version of Java compared to when run from the command line. 原因是因为与从命令行运行时相比,您的Eclipse实例使用不同版本的Java。 In order to check which java version is being used as default, type java -version on the command prompt 为了检查哪个Java版本被用作默认版本, java -version在命令提示符下键入java -version

C:>java -version

java version "1.6.0_45" Java(TM) SE Runtime Environment (build 1.6.0_45-b06) Java HotSpot(TM) Client VM (build 20.45-b01, mixed mode, sharing)

In order to check the Java version that your eclipse instance uses, check out 为了检查您的eclipse实例使用的Java版本,请签出

Welcome Project-->Properties--> Java Build Path--> Libraries--> JRE System Library.

You can verify the Java version for your project from the package explorer as well. 您也可以从包资源管理器中验证项目的Java版本。 在此处输入图片说明

Right click on your project --> properties and change version in below screen 右键单击您的项目->属性,然后在下面的屏幕中更改版本

在此处输入图片说明

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

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