Java 在 NetBeans 中找不到 Main 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20601845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No Main class found in NetBeans
提问by Luis Ramos
I have been working on an assignment for my class in programming. I am working with NetBeans. I finished my project and it worked fine. I am getting a message that says "No main class found" when I try to run it. Here is some of the code with the main:
我一直在为我的编程课做作业。我正在使用 NetBeans。我完成了我的项目,它运行良好。当我尝试运行它时,我收到一条消息,显示“找不到主类”。下面是一些主要的代码:
package luisrp3;
import java.io.FileNotFoundException;
import java.io.PrintStream;
public class LuisRp3 {
public static void main(String[] args) throws FileNotFoundException {
java.io.File newFile = new java.io.File("LuisRamosp4.txt");
if (newFile.exists()) {
newFile.delete();
}
System.setOut(new PrintStream(newFile));
Guitar guitar = new Guitar();
I posted this before but had a couple issues. i have fixed the others and now have just this one remaining. Any advice will be greatly appreciated.
我之前发布过这个,但有几个问题。我已经修复了其他的,现在只剩下这个了。任何建议将不胜感激。
采纳答案by Paul Samsotha
- Right click on your Project in the project explorer
- Click on properties
- Click on Run
- Make sure your Main Class is the one you want to be the entry point. (Make sure to use the fully qualified name i.e. mypackage.MyClass)
- Click OK.
- Run Project :)
- 在项目资源管理器中右键单击您的项目
- 点击属性
- 点击运行
- 确保您的主类是您想要作为入口点的类。(确保使用完全限定名称,即 mypackage.MyClass)
- 单击确定。
- 运行项目:)
If you just want to run the file, right click on the class from the package explorer, and click Run File, or (Alt+ R, F), or (Shift+ F6)
如果您只想运行该文件,请在包资源管理器中右键单击该类,然后单击“运行文件”,或 ( Alt+ R, F) 或 ( Shift+ F6)
回答by Yehia Awad
if all that is your code you forgot to close the main method
如果所有这些都是你的代码,你忘记关闭 main 方法
everything else sounds good to me
其他一切对我来说都很好
public class LuisRp3 {
public static void main(String[] args) throws FileNotFoundException {
java.io.File newFile = new java.io.File("LuisRamosp4.txt");
if (newFile.exists()) {
newFile.delete();
}
System.setOut(new PrintStream(newFile));
Guitar guitar = new Guitar();
}}
try that
试试看
回答by Foster Hao
You need to add }}
to the end of your code.
您需要添加}}
到代码的末尾。
回答by tamul
If the advice to add the closing braces work, I suggest adding indentation to your code so every closing brace is on a spaced separately, i.e.:
如果添加右大括号的建议有效,我建议在您的代码中添加缩进,以便每个右大括号都分开放置,即:
public class LuisRp3 {
public static void main(String[] args) throws FileNotFoundException {
// stuff
}
}
This just helps with readability.
这只是有助于提高可读性。
If, on the other hand, you just forgot to copy the closing braces in your code, or the above suggestion doesn't work: open up the configuration and see if you can manually set the main class. I'm afraid I haven't used NetBeans much, so I can't help you with where that option is. My best guess is under "Run Configuration", or something like that.
另一方面,如果您只是忘记复制代码中的右大括号,或者上述建议不起作用:打开配置,看看是否可以手动设置主类。恐怕我没有经常使用 NetBeans,所以我无法帮助您了解该选项的位置。我最好的猜测是在“运行配置”下,或者类似的东西。
Edit:See peeskillet's answerif adding closing braces doesn't work.
编辑:如果添加右大括号不起作用,请参阅peeskillet 的答案。
回答by EpicDavi
There could be a couple of things going wrong in this situation (assuming that you had code after your example and didn't just leave your code unbracketed).
在这种情况下可能会出现一些问题(假设您在示例之后有代码并且不只是让您的代码未加括号)。
First off, if you are running your entire project and not just the current file, make sure your project is the main project and the main class of the project is set to the correct file.
首先,如果您正在运行整个项目而不仅仅是当前文件,请确保您的项目是主项目并且项目的主类设置为正确的文件。
Otherwise, I have seen classmates with their code being fine but they still had this same problem. Sometimes, in Netbeans, a simple fix is to:
否则,我看到同学的代码很好,但他们仍然遇到同样的问题。有时,在 Netbeans 中,一个简单的解决方法是:
- Copy your current code (or back it up in a different location)
- Delete your current file
- Create a new main class in your project (you can name it the old one)
- Paste your code back in
- 复制您当前的代码(或将其备份到其他位置)
- 删除当前文件
- 在您的项目中创建一个新的主类(您可以将其命名为旧的)
- 将您的代码粘贴回
If this doesn't work then try to clear the Netbeans cache, and if all else fails, then just do a clean un-installation and re-installation of Netbeans.
如果这不起作用,则尝试清除 Netbeans 缓存,如果所有其他方法都失败,则只需彻底卸载并重新安装 Netbeans。
回答by Stefan
I had the same problem in Eclipse, so maybe what I did to resolve it can help you. In the project properties I had to set the launch configurations to the file that contains the main-method (I don't know why it wasn't set to the right file automatically).
我在 Eclipse 中遇到了同样的问题,所以也许我为解决它所做的工作可以帮助你。在项目属性中,我必须将启动配置设置为包含 main-method 的文件(我不知道为什么它没有自动设置为正确的文件)。
回答by TriMix
Also, for others out there with a slightly different problem where Netbeans will not find the class when you want when doing a browse from "main classes dialog window".
此外,对于其他有稍微不同的问题,当您从“主类对话框窗口”进行浏览时,Netbeans 将找不到该类。
It could be that your main method does have the proper signature. In my case I forgot the args.
可能是您的主要方法确实具有正确的签名。就我而言,我忘记了参数。
example: public static void main(String[] args)
示例:public static void main(String[] args)
The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above.
修饰符 public 和 static 可以按任意顺序编写(public static 或 static public),但约定是使用如上所示的 public static。
Args: You can name the argument anything you want, but most programmers choose "args" or "argv".
Args:您可以随意命名参数,但大多数程序员选择“args”或“argv”。
Read more here: http://docs.oracle.com/javase/tutorial/getStarted/application/
在此处阅读更多信息:http: //docs.oracle.com/javase/tutorial/getStarted/application/
回答by Asmat Ali
In project properties, under the run tab, specify your main class. Moreover, To avoid this issue, you need to check "Create main class" during creating new project. Specifying main class in properties should always work, but if in some rare case it doesn't work, then the issue could be resolved by re-creating the project and not forgetting to check "Create main class" if it is unchecked.
在项目属性的运行选项卡下,指定您的主类。此外,为避免此问题,您需要在创建新项目时勾选“创建主类”。在属性中指定主类应该始终有效,但如果在极少数情况下它不起作用,则可以通过重新创建项目来解决问题,并且如果未选中,不要忘记选中“创建主类”。
回答by Hilary Brobbey
Make sure the access modifier is publicand not private. I keep having this problem and always that's my issue.
确保访问修饰符是公共的而不是私有的。我一直有这个问题,而且总是我的问题。
publicstatic void main(String[] args)
publicstatic void main(String[] args)
回答by OmShanti
You need to rename your main class to Main
, it cannot be anything else.
您需要将主类重命名为Main
,它不能是其他任何东西。
It does not matter how many files as packages and classes you create, you must name your main class Main
.
无论您创建多少文件作为包和类,您都必须将主类命名为Main
.
That's all.
就这样。