简体   繁体   English

Eclipse启动了错误的.Class文件

[英]Eclipse Launches Wrong .Class File

I'm relatively new to Java and I've been having a recurring problem that's been frustrating me. 我对Java比较陌生,而且我一直有一个令我感到沮丧的反复出现的问题。

I have two class files in the same Project folder: 'Main.java' & 'Client.java'. 我在同一个Project文件夹中有两个类文件:'Main.java'和'Client.java'。

'Main.java' is the server ( I run this first). 'Main.java'是服务器(我先运行它)。 I try to run Client.java to connect to the server. 我尝试运行Client.java以连接到服务器。 However, it keeps re-launching 'Main.java' regardless of my attempts to fix this. 但是,无论我试图解决这个问题,它都会不断重新启动“Main.java”。 I have tried tried selecting 'Run As' and 'Run Configuration..' but nothing seems to work. 我试过尝试选择'Run As'和'Run Configuration ..'但似乎没有任何效果。 This has happened me in several projects and I cannot seem to figure out a solution. 这在几个项目中发生了我,我似乎无法找到解决方案。

Here is my code: 这是我的代码:

1: Main.java 1:Main.java

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;

public class Main {

    public static void main(String[] args) throws IOException {
        try {
            final int PORT = 6677;
            ServerSocket server = new ServerSocket(PORT);
            System.out.println("Waiting for clients...");

            while (true) {
                Socket s = server.accept();

                System.out.println("Client connected from "
                        + s.getLocalAddress().getHostName());

                Client chat = new Client(s);
                Thread t = new Thread(chat);
                t.start();
            }
        } catch (Exception e) {
            System.out.println("An error occured.");
            e.printStackTrace();
        }
    }

}

2: Client.java 2:Client.java

import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Client implements Runnable {

    private Socket socket;

    public Client(Socket s) {
        socket = s;
    }

    @Override
    public void run() {
        try {
            Scanner in = new Scanner(socket.getInputStream());
            PrintWriter out = new PrintWriter(socket.getOutputStream());

            while (true) {
                if (in.hasNext()) {
                    String input = in.nextLine();
                    System.out.println("Client Said: " + input);
                    out.println("You Said: " + input);
                    out.flush();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Thanks. 谢谢。

That's because the execution of a program (no matter how many classes it has inside) always start with the class containing the "main()" function. 那是因为程序的执行(无论它里面有多少个类)总是从包含“main()”函数的类开始。 As you can see, Main.java is the file holding the main() function and hence execution of this program always starts with that. 如您所见,Main.java是包含main()函数的文件,因此该程序的执行始终以此开头。 One of the simplest solutions (not the best) would be to create instances of client in the main function. 最简单的解决方案之一(不是最好的)是在main函数中创建客户端实例。 Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM