主函数清楚地提到,但出现错误:在类”Server”中找不到主方法。

huangapple go评论81阅读模式
英文:

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(&quot;Unable to open socket&quot;);
	}
}

}


</details>



# 答案2
**得分**: -1

我认为你需要将服务器类设置为公共类。

<details>
<summary>英文:</summary>

i think you have to make server class as public.

</details>



huangapple
  • 本文由 发表于 2020年9月20日 04:24:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63973107.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定