简体   繁体   English

无法在Eclipse中运行“Hello World”程序

[英]Cannot run “Hello World” program in Eclipse

I have an error in my first step with Java, so when i try to run the code hello world: 我在使用Java的第一步中出错,所以当我尝试运行代码hello world时:

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

I go to: - Run as .. -> Then i choose Java aplicacion - > And i press Ok 我去: - 运行为.. - >然后我选择Java aplicacion - >然后按Ok

But when i press Ok does not appear the window down to show me the correct message Hello World 但是,当我按下确定时,窗口中没有显示正确的消息Hello World

Your code works fine for me: 你的代码对我来说很好:

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

I downloaded it to c:\\temp\\apples.java. 我将它下载到c:\\ temp \\ apples.java。

Here's how I compiled and ran it: 这是我编译和运行它的方式:

C:\temp>javac -cp . apples.java

C:\temp>dir apples
 Volume in drive C is HP_PAVILION
 Volume Serial Number is 0200-EE0C

 Directory of C:\temp

C:\temp>dir ap*
 Volume in drive C is HP_PAVILION
 Volume Serial Number is 0200-EE0C

 Directory of C:\temp

08/15/2010  09:15 PM               418 apples.class
08/15/2010  09:15 PM               123 apples.java
               2 File(s)            541 bytes
               0 Dir(s)  107,868,696,576 bytes free

C:\temp>java -cp . apples
Hello World!

C:\temp>

Your lack of understanding and the IDE appear to be impeding your progress. 您缺乏理解和IDE似乎阻碍了您的进步。 Do simple things without the IDE for a while until you get the hang of it. 在没有IDE的情况下做一些简单的事情,直到你掌握它为止。 A command shell and a text editor will be sufficient. 命令shell和文本编辑器就足够了。

Sorry about missing javac; 抱歉失踪的javac; cut & paste error. 剪切和粘贴错误。

If you look at the screenshot, your class name is there, last in the list. 如果您查看屏幕截图,您的班级名称就在那里,列表中的最后一个。 Select it and press OK. 选择它并按OK。 To not see this message again, right-click on the class name on the left side and select there Run...->Java Application. 要再次看不到此消息,请右键单击左侧的类名,然后选择Run ...-> Java Application。

The only problem that causes your error here is that the classname and the filename do not match - and they have to. 导致您的错误的唯一问题是 文件名不匹配 - 他们必须这样做。

Solution

Rename either the file thesame.java to apple.java or the class to thesame . 或者重命名文件thesame.javaapple.java或类thesame Then if you select "Run as..." again, eclipse will present a menu item to start your Java application. 然后,如果再次选择“Run as ...”,eclipse将显示一个菜单项以启动Java应用程序。

(other mentioned, that there's no requirement that a top-level class and the filename do match - unless the top level class is public. Of course this is true. But the problem was about "running" a class under eclipse as a Java application) (其他提到,没有要求顶级类和文件名匹配 - 除非顶级类是公共的。当然这是真的。但问题是关于在eclipse下“运行”一个类作为Java应用程序)

Try public class apples and make sure the file is apples.java. 尝试public class apples Apple并确保该文件是apples.java。 Also it should be public static void main(String[] args) 它也应该是public static void main(String[] args)

Class names must be capitalized... so change apples to Apples. 类名必须大写...所以将 apples更改为Apples。 Also, if you are a beginner (which it seems like), I would recommend the Netbeans IDE -- it's a bit more friendlier for new users than Eclipse. 另外,如果你是初学者(看起来像),我会推荐Netbeans IDE--对于新用户来说,它比Eclipse更友好一些。

You have 2 classes by name of "thesame.java" under the source folder. 源文件夹下有两个名为“thesame.java”的类。 Since one is directly under the src folder, and other under (default package), they use the same namespace, hence Interpreter is confused which java file to execute and is asking you to select the class you want to execute. 由于一个直接位于src文件夹下,而另一个位于(默认包)下,因此它们使用相同的命名空间,因此Interpreter会混淆要执行哪个java文件并要求您选择要执行的类。

  • You class must be named "thesame" if you store it in a file called "thesame.java", as you have. 如果您将类存储在名为“thesame.java”的文件中,则必须将该类命名为“thesame”。 Either rename your class to "thesame" or change the file to be "apples.java". 将您的类重命名为“thesame”或将文件更改为“apples.java”。

  • You should move the "[]" to be before "args". 你应该将“[]”移到“args”之前。 So, String[] args . 所以, String[] args

  • Either select "apples" at the bottom of the menu you posted and run it, or right-click on the Java file and make it the default thing to run for this project. 在您发布的菜单底部选择“apples”并运行它,或右键单击Java文件并使其成为此项目运行的默认设置。 Or launch it by right-clicking on the file and selecting "run". 或者通过右键单击文件并选择“运行”来启动它。

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

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