我在这里错过了什么或者我应该做不同的事情?
What am i missing here or i should be doing differently?
我刚刚开始学习 Java 中的 类,所以我不确定我在这里遗漏了什么或者我应该做些什么不同的事情。
这就是我正在做的事情:
class Student {
private int roll;
private String name;
public void hW(){
System.out.println("Hello world");
}
}
public class classes {
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
System.out.println("hello");
Student s = new Student();
s.hW();
// TODO code application logic here
}
}
当我 运行 这个文件时出现这个错误:
error: can't find main(String[]) method in class: Student
我应该做哪些改变??
我在 macOSX
上使用 Netbeans IDE 11.3
虽然您在问题中没有提到您是如何尝试 运行 该文件的,但我能够通过一些点击和试用来重现该问题。以下是您尝试执行的操作以及遇到问题的原因:
- 您已经在 IDE 中创建了
classes.java
,并且在此文件中,您输入了您发布的代码。
- 现在,您将前往 Mac OS 航站楼 window 并尝试 运行
java classes.java
可能会想到 Java 11 功能允许您 运行 一个单文件程序而无需编译 .
您为什么遇到这个问题?
你遇到这个问题是因为 class Student
在顶部但它没有 public static void main(String[] args)
而 Java 的这个功能需要顶级 class可执行文件(即具有此 main
方法)
检查以下来自 https://openjdk.java.net/jeps/330
的段落
In source-file mode, execution proceeds as follows:
The class to be executed is the first top-level class found in the
source file. It must contain a declaration of the standard public
static void main(String[]) method.
解决方法是什么?
以下任何一项都有效:
- 将 class、
classes
向上移动,使其成为文件中的第一个 class。由于 class、classes
已经有 public static void main(String[] args)
,您的命令 java classes.java
将毫无问题地执行它。
- 将以下代码放入
class Student
并从终端 window. 执行 java classes.java
public static void main(String args[]) {
System.out.println("hello");
Student1 s = new Student1();
s.hW();
}
- 从 Mac OS 终端 window 执行以下命令到 运行 你的 class 在 pre-Java11 方式:
javac classes.java
java classes
- 从您的IDE执行文件。
附带说明,您应该遵循 Java naming conventions 例如根据命名约定,class、classes
应该是 Classes
。
我刚刚开始学习 Java 中的 类,所以我不确定我在这里遗漏了什么或者我应该做些什么不同的事情。 这就是我正在做的事情:
class Student {
private int roll;
private String name;
public void hW(){
System.out.println("Hello world");
}
}
public class classes {
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
System.out.println("hello");
Student s = new Student();
s.hW();
// TODO code application logic here
}
}
当我 运行 这个文件时出现这个错误:
error: can't find main(String[]) method in class: Student
我应该做哪些改变??
我在 macOSX
上使用 Netbeans IDE 11.3虽然您在问题中没有提到您是如何尝试 运行 该文件的,但我能够通过一些点击和试用来重现该问题。以下是您尝试执行的操作以及遇到问题的原因:
- 您已经在 IDE 中创建了
classes.java
,并且在此文件中,您输入了您发布的代码。 - 现在,您将前往 Mac OS 航站楼 window 并尝试 运行
java classes.java
可能会想到 Java 11 功能允许您 运行 一个单文件程序而无需编译 .
您为什么遇到这个问题?
你遇到这个问题是因为 class Student
在顶部但它没有 public static void main(String[] args)
而 Java 的这个功能需要顶级 class可执行文件(即具有此 main
方法)
检查以下来自 https://openjdk.java.net/jeps/330
的段落In source-file mode, execution proceeds as follows:
The class to be executed is the first top-level class found in the source file. It must contain a declaration of the standard public static void main(String[]) method.
解决方法是什么?
以下任何一项都有效:
- 将 class、
classes
向上移动,使其成为文件中的第一个 class。由于 class、classes
已经有public static void main(String[] args)
,您的命令java classes.java
将毫无问题地执行它。 - 将以下代码放入
class Student
并从终端 window. 执行
java classes.java
public static void main(String args[]) {
System.out.println("hello");
Student1 s = new Student1();
s.hW();
}
- 从 Mac OS 终端 window 执行以下命令到 运行 你的 class 在 pre-Java11 方式:
javac classes.java
java classes
- 从您的IDE执行文件。
附带说明,您应该遵循 Java naming conventions 例如根据命名约定,class、classes
应该是 Classes
。