简体   繁体   English

Eclipse包需要成功运行

[英]Eclipse package needed to run successfully

I'm just beginning at running simple Java programs with Eclipse. 我刚刚开始使用Eclipse运行简单的Java程序。 The udemy.com tutorial I'm working from starts each program with public class followed by the main method. 我正在工作的udemy.com教程从每个程序开始都是公共类,然后是main方法。 I get an error in main method compilation error, unless I start a file from scratch and include the package line at the beginning. 我在主方法编译错误中遇到错误,除非我从头开始创建文件并在开始处包含打包行。 It runs fine when package is included. 包含软件包时,它运行良好。 I don't understand why this is necessary. 我不明白为什么这样做是必要的。 Most of the tutorials and example code I've looked at do not include the package line. 我看过的大多数教程和示例代码都不包含打包行。

Many tutorials skip parts of the code for the sake of brevity, such as the package declaration and imports. 为了简洁起见,许多教程都跳过了部分代码,例如包声明和导入。 However, the package should ALWAYS be declared at the top of the class if the class is inside of a package inside your source directory. 但是,如果该类在源目录中的某个包内,则应始终在该类的顶部声明该包。 The only time you don't declare the package is if the class is directly inside your source directory (considered the default package). 唯一不声明该程序包的情况是,该类是否直接位于源目录中(被视为默认程序包)。 However as the comments mention - this is not a very good practice. 但是,正如评论所提到的那样,这不是一个很好的做法。 Also, you shouldn't need to start a file from scratch - just add the package declaration at the top and save the file - which should trigger a compile. 另外,您不需要从头开始文件-只需在顶部添加包声明并保存文件-这将触发编译。 If it doesn't - make sure you have Project > Build Automatically enabled. 如果不是,请确保已启用“项目”>“自动生成”。

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

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