吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1247|回复: 15
上一主题 下一主题
收起左侧

[求助] Error: Can't find main method in class Hello, define main method as: Public s...

[复制链接]
跳转到指定楼层
楼主
allrobot 发表于 2021-10-28 21:20 回帖奖励
如题,今天用notepad++写java程序,用nppExec插件跑一下,但突然给我报错这段信息
我尝试了各种办法没有什么效果,我写了hello居然报错,不明白是什么原因导致的...明明不久前运行一切正常

如上图,我用cmd执行java Hello,cmd报错:
[Asm] 纯文本查看 复制代码
C:\Users\li1gu\Desktop\Schoolwork\JAVA_1761293\java程序设计实验(2021)\例题>java Hello
错误: 在类 Hello 中找不到 main 方法, 请将 main 方法定义为:
   public static void main(String[] args)
否则 JavaFX 应用程序类必须扩展javafx.application.Application


查看版本,没啥错啊
[Asm] 纯文本查看 复制代码
C:\Users\li1gu\Desktop\Schoolwork\JAVA_1761293\java程序设计实验(2021)\例题>java -version
java version "1.8.0_301"
Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.301-b09, mixed mode)

Path也没什么毛病

用intellij idea正常运行
到cmd或Notepa++(nppExec本质是使用cmd执行的)就不行了,什么原因?求解决方案

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

沙发
 楼主| allrobot 发表于 2021-10-28 21:22 |楼主
版主,我好像发错版块了,求转移板块
3#
沙滩上de水瓶 发表于 2021-10-28 22:47
如果notepad里面写的那个Hello.java是没有包名的,是没有问题的。
不过你为什么要用notepad写java,文艺复兴嘛
4#
EnterpriseSolu 发表于 2021-10-29 07:42
应该文件字符编码错误,就是asci, unicode, utf-8这种编码,好久没做JAVA,不记得要用那种格式,
用Eclipse, Java Builder, Android Studio,IntelliJ等工具,会为你处理好这些细节,这些小细节磨死人了,小建议是不用notepad,有挖掘机可以,为啥还要用小锄头,又累又容易出错
5#
EnterpriseSolu 发表于 2021-10-29 07:45
找到答案了,推荐用UTF-8
https://stackoverflow.com/questi ... -a-java-source-file

1

These differences usually have at least one of the following reasons:

Source encoding
Java does not have an encoding defined in the source file, so you have to agree with your team members. Usually there's no good reason to choose anything else than UTF-8. If your preferred editor only supports system encoding... choose another one.
6#
 楼主| allrobot 发表于 2021-10-29 08:18 |楼主
沙滩上de水瓶 发表于 2021-10-28 22:47
如果notepad里面写的那个Hello.java是没有包名的,是没有问题的。
不过你为什么要用notepad写java,文艺复 ...

有时intellij速度比较慢,notepad++写起来更快一些

cmd运行java,当然不能写包名的
7#
 楼主| allrobot 发表于 2021-10-29 08:26 |楼主
EnterpriseSolu 发表于 2021-10-29 07:42
应该文件字符编码错误,就是asci, unicode, utf-8这种编码,好久没做JAVA,不记得要用那种格式,
用Eclips ...

小工具用的比较多,因为不是很常写大项目的


notepad++页面默认utf-8编码的,nppexec使用utf-8模式进行编译运行的,我尝试过ANSI编码,但没啥用
8#
 楼主| allrobot 发表于 2021-10-29 08:27 |楼主
EnterpriseSolu 发表于 2021-10-29 07:45
找到答案了,推荐用UTF-8
https://stackoverflow.com/questions/62511914/encoding-of-a-java-source-file ...

可是,notepad++默认uft-8编码的啊
9#
沙滩上de水瓶 发表于 2021-10-29 10:33
allrobot 发表于 2021-10-29 08:18
有时intellij速度比较慢,notepad++写起来更快一些

cmd运行java,当然不能写包名的

1、说没问题就是没问题,我自己都试了一遍,实在有问题就是别的地方出错了或者你贴错代码了,不信就去问你的老师。
2、cmd运行java,可以写包名。
3、你上面的代码全是英文,用什么文件编码都是一样的,编码不一样顶多是乱码。
4、你在stack overflow上面找的那个问题,人家问的是:“为什么同样编码的java文件,提交了之后git认为整个文件都改变了,而BeyondCompare显示只有换行符不一样”,和你的问题根本不是同一个问题。
10#
EnterpriseSolu 发表于 2021-10-29 13:04
试下这个
https://stackoverflow.com/questi ... -the-main-method-as
既然能看懂英文,bing一下,几分钟就解决问题,
When you use the java command to run a Java application from the command line, e.g.,

java some.AppName arg1 arg2 ...
the command loads the class that you nominated and then looks for the entry point method called main. More specifically, it is looking for a method that is declared as follows:

package some;
public class AppName {
    ...
    public static void main(final String[] args) {
        // body of main method follows
        ...
    }
}
The specific requirements for the entry point method are:

The method must be in the nominated class.
The name of the method must be "main" with exactly that capitalization1.
The method must be public.
The method must be static 2.
The method's return type must be void.
The method must have exactly one argument and argument's type must be String[] 3.
(The argument may be declared using varargs syntax; e.g. String... args. See this question for more information. The String[] argument is used to pass the arguments from the command line, and is required even if your application takes no command-line arguments.)

If anyone of the above requirements is not satisfied, the java command will fail with some variant of the message:

Error: Main method not found in class MyClass, please define the main method as:
   public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application
Or, if you are running an extremely old version of Java:

java.lang.NoSuchMethodError: main
Exception in thread "main"
If you encounter this error, check that you have a main method and that it satisfies all of the six requirements listed above.

1 - One really obscure variation of this is when one or more of the characters in "main" is NOT a LATIN-1 character … but a Unicode character that looks like the corresponding LATIN-1 character when displayed.
2 - Here is an explanation of why the method is required to be static.
3 - String must correspond to java.lang.String and not to a custom class named String hiding it.
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 22:30

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表