You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Eclipse环境下Java Server Socket类编译错误:需添加}完成类体

问题解决:Java编译错误“Syntax error, insert "}" to complete ClassBody”

错误原因

你的代码存在两处问题:

  1. comunica()方法定义末尾多了一个冗余分号};,Java方法定义不需要以分号结尾
  2. 整个ServerStr类的末尾缺少闭合的},导致类体结构不完整

修正后的完整代码

import java.io.*;
import java.net.*;
import java.util.*;

public class ServerStr {
    ServerSocket server = null;
    Socket client = null;
    String stringaRicevuta = null;
    String stringaModificata = null; 
    BufferedReader inDalClient;
    DataOutputStream outVersoClient; 

    public Socket attendi() {
        try {
            System.out.println("1 SERVER partito in esecuzione..partito.");
            server = new ServerSocket(6789);
            client = server.accept();
            server.close();
            inDalClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
            outVersoClient = new DataOutputStream(client.getOutputStream());
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            System.out.println("Errore durante l'istanza del server");
            System.exit(1);
        }
        return client;
    }

    public void comunica() {
        try {
            System.out.println("3 benvenuto client, scrivi una frase e la trasformo in maiuscolo. Attendo...");
            stringaRicevuta = inDalClient.readLine();
            System.out.println(" e ricevuta la stringa dal cliente" + stringaRicevuta);
            stringaModificata = stringaRicevuta.toUpperCase();
            System.out.println(" invio la stringa modificata al client...");
            outVersoClient.writeBytes(stringaModificata + '\n');
            System.out.println("9 SERVER: fine elaborazione... buonanotte!");
            client.close();
        }
        finally {
        }
    }

    public static void main(String args[]) {
        ServerStr servente = new ServerStr();
        servente.attendi();
        servente.comunica();
    }
}

关键修改点

  • 移除comunica()方法末尾的多余分号;
  • 在main()方法的闭合}之后,添加一个}来完成ServerStr类的类体闭合

内容的提问来源于stack exchange,提问作者Simone Sarugia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 13:10:49