英文:
main function clearly mentioned but getting Error: Main method not found in class Server
问题
在一个系统上创建了一个名为Server.java的文件,它将在一个无限循环中等待连接请求。
Server.java文件中有一个已定义正确签名的main函数,但我仍然遇到以下错误。
> 错误:在类Server中找不到main方法,请定义main方法,如下:
> public static void main(String[] args) 或者JavaFX应用程序类必须扩展javafx.application.Application
class Server {
final static int TRACKER_PORT = 5000;
public static void main(String args[]) {
try (ServerSocket s = new ServerSocket(TRACKER_PORT)) {
System.out.println("The tracker server is running on port " + TRACKER_PORT);
Tracker t = new Tracker();
while(true) {
Socket cl = s.accept();
new Handler(cl,t);
}
}
catch(Exception e) {
System.out.println("Unable to open socket");
}
}
}
亲爱的各位,感谢你们的时间和努力。上述问题已解决。这是因为在我设置为CLASSPATH的某个路径中也存在一个Server.class文件,因此编译器在那个文件中寻找main函数,但那个文件中并没有任何main函数。我在删除了那个CLASSPATH后重新运行了代码,现在代码正常运行。
英文:
Created a file Server.java which will run on a System and will wait for connection request(in an infinite loop).
This file in Server.java has main funciton defined with correct signature but still I am getting following error.
> Error: Main method not found in class Server, please define the main
> method as: public static void main(String[] args) or a JavaFX
> application class must extend javafx.application.Application
class Server {
final static int TRACKER_PORT = 5000;
public static void main(String args[]) {
try (ServerSocket s = new ServerSocket(TRACKER_PORT)) {
System.out.println("The tracker server is running on port " + TRACKER_PORT);
Tracker t = new Tracker();
while(true) {
Socket cl = s.accept();
new Handler(cl,t);
}
}
catch(Exception e) {
System.out.println("Unable to open socket");
}
}
Dear All, Thank you for your time and efforts. Above problems has been solved. It occured because a Server.class also existed at one of the path I set as CLASSPATH. so compiler was looking for main function in that file and that file doesn't had any main file. I re-run the code after removing that CLASSPATH and now code is running fine.
答案1
得分: 1
主方法缺少花括号。
class Server {
final static int TRACKER_PORT = 5000;
public static void main(String args[]) {
try(ServerSocket s = new ServerSocket(TRACKER_PORT)) {
System.out.println("Tracker 服务器正在端口上运行:" + TRACKER_PORT);
Tracker t = new Tracker();
while(true) {
Socket cl = s.accept();
new Handler(cl, t);
}
} catch(Exception e) {
System.out.println("无法打开套接字");
}
}
}
<details>
<summary>英文:</summary>
The main method is missing curly braces.
class Server {
final static int TRACKER_PORT = 5000;
public static void main(String args[]) {
try(ServerSocket s = new ServerSocket(TRACKER_PORT)) {
System.out.println("The tracker server is running on port " + TRACKER_PORT);
Tracker t = new Tracker();
while(true) {
Socket cl = s.accept();
new Handler(cl, t);
}
} catch(Exception e) {
System.out.println("Unable to open socket");
}
}
}
</details>
# 答案2
**得分**: -1
我认为你需要将服务器类设置为公共类。
<details>
<summary>英文:</summary>
i think you have to make server class as public.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论