2014-09-03 117 views
-1

我正在學習在Java中使用套接字。我在網上得到了這個例子,並試圖實現它。它是一個猜測數字的遊戲。服務器獲取一個隨機數供用戶從客戶端猜測。我在Eclipse 4.2.2中構建了這個項目。但是,當我試圖啓動從MAC終端服務器,它顯示了這一點:在Java套接字編程中發生「線程異常」主「java.lang.NoClassDefFoundError」

ritekiMacBook-Pro:test Michael$ javac ServerApp.java 
ServerApp.java:45: ?Ҳ??????? 
???ţ? ???? ClientApp 
λ?ã? ?? test.ServerApp.ServerThread 
       ClientApp.finished = true; 
       ^
ServerApp.java:48: ?Ҳ??????? 
???ţ? ???? ClientApp 
λ?ã? ?? test.ServerApp.ServerThread 
       ClientApp.finished = false; 
       ^
ServerApp.java:51: ?Ҳ??????? 
???ţ? ???? ClientApp 
λ?ã? ?? test.ServerApp.ServerThread 
       ClientApp.finished = false; 
       ^
ServerApp.java:54: ?Ҳ??????? 
???ţ? ???? ClientApp 
λ?ã? ?? test.ServerApp.ServerThread 
       if (!ClientApp.finished){ 
       ^
ServerApp.java:60: ?Ҳ??????? 
???ţ? ???? ClientApp 
λ?ã? ?? test.ServerApp.ServerThread 
      if (!ClientApp.finished){ 
       ^
ServerApp.java:63: ?Ҳ??????? 
???ţ? ???? ClientApp 
λ?ã? ?? test.ServerApp.ServerThread 
      ClientApp.finished = true; 
      ^
6 ???? 



ritekiMacBook-Pro:test Michael$ java ServerApp 
Exception in thread "main" java.lang.NoClassDefFoundError: ServerApp 
Caused by: java.lang.ClassNotFoundException: ServerApp 
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 

而這也恰好給ClientApp.java。 我完全對此感到困惑。我不確定它是因爲我MAC的默認語言是中文。

這裏是ServerApp和ClientApp代碼:

ServerApp.java:

package test; 
    import java.io.*; 
    import java.net.*; 

    public class ServerApp{ 
     static final int PORT = 9000; 
     private ServerSocket serverSocket; 
     private Socket socket; 
     private BufferedReader netIn; 
     private PrintWriter netOut; 
     public ServerApp() throws IOException{ 
     serverSocket = new ServerSocket(PORT); 
     System.out.println("server start"); 
     while (true){ 
      // waiting for connect; 
      socket = serverSocket.accept(); 
      ServerThread st = new ServerThread(socket); 
      new Thread(st).start(); 
     } 
     } 
     class ServerThread implements Runnable{ 
     private Socket socket; 
     private int randomNumber; 
     private int clientGuessNumber; 
     public ServerThread(Socket s) throws IOException{ 
      socket = s; 
      netIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
      netOut = new PrintWriter(socket.getOutputStream()); 
     } 
     public void run(){ 
      System.out.println("client" + socket.getInetAddress() + " is connect"); 
      randomNumber = (int) (Math.random() * 100); 
      System.out.println("random number is:" + randomNumber); 
      try{ 
      clientGuessNumber = Integer.parseInt(netIn.readLine()); 
      System.out.println("client guess number is:" + clientGuessNumber); 
      for (int i = 3; i > 0; i--){ 
       if (clientGuessNumber == randomNumber){ 
       netOut.println("OK, you got it right"); 
       ClientApp.finished = true; 
       } else if (clientGuessNumber < randomNumber){ 
       netOut.println("Your number is smaller than it, you still have " + (i - 1) + "chances"); 
       ClientApp.finished = false; 
       } else if (clientGuessNumber > randomNumber){ 
       netOut.println("Your number is bigger than it, you still have" + (i - 1) + "chances"); 
       ClientApp.finished = false; 
       } 
       netOut.flush(); 
       if (!ClientApp.finished){ 
       clientGuessNumber = Integer.parseInt(netIn.readLine()); 
       } else{ 
       break; 
       } 
      } 
      if (!ClientApp.finished){ 
       netOut.println("OK, game over. You used all your chances."); 
      } 
      ClientApp.finished = true; 
      } catch (IOException e){ 
      } finally{ 
      try{ 
       netOut.close(); 
       netIn.close(); 
       socket.close(); 
      } catch (IOException ee){ 
      } 
      } 
     } 
     } 
     public static void main(String[] args) throws IOException{ 
     new ServerApp(); 
     } 
    } 

ClientApp.java:

package test; 
import java.io.*; 
import java.net.*; 

public class ClientApp{ 
      private Socket socket; 
      private BufferedReader netIn; 
      private PrintWriter netOut; 
      private BufferedReader keyboardIn; 
      static Boolean finished = false; 

      public ClientApp() throws IOException{ 
      System.out.println("Please input the server address, for local server please input localhost"); 
      keyboardIn = new BufferedReader(new InputStreamReader(System.in)); 
      try{ 
       if (keyboardIn.readLine().equalsIgnoreCase("localhost")){ 
       socket = new Socket(InetAddress.getLocalHost(), ServerApp.PORT); 
       } else{ 
       socket = new Socket(InetAddress.getByName(keyboardIn.readLine()), ServerApp.PORT); 
       } 
       System.out.println(ServerApp.PORT); 
       netIn = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
       netOut = new PrintWriter(socket.getOutputStream()); 
      } catch (UnknownHostException e){ 
       System.err.println("cannot connect to server"); 
       System.exit(-1); 
      } 
      System.out.println("Connect suceeded"); 
      while (!finished){ 
       System.out.println("Please input a number between 0 and 100"); 
       netOut.println(keyboardIn.readLine()); 
//   netOut.print(keyboardIn.readLine()); 
       netOut.flush(); 
//   System.out.println(ServerApp.PORT); 
       String str = netIn.readLine(); 
       if (str == null){ 
       finished = true; 
       break; 
       } 
       System.out.println(str); 
       if (str.startsWith("OK")){ 
       finished = true; 
       break; 
       } 
      } 
      netIn.close(); 
      netOut.close(); 
      keyboardIn.close(); 
      socket.close(); 
      } 
      public static void main(String[] args) throws IOException{ 
      new ClientApp(); 
      } 

} 

有人能幫助我嗎?謝謝。

+0

檢查,如果您使用的是保存版本的編譯和運行你的代碼。 – Hannes 2014-09-03 18:53:15

+0

是的,他們都是1.6.0_65版本。 – 2014-09-03 19:20:18

+0

該編譯器輸出錯誤!你期望什麼? – chrylis 2014-09-03 19:51:18

回答

2

您正在test文件夾中運行java命令。改爲父目錄並改爲運行。

Java編譯器希望在與包結構匹配的目錄結構中查找類。您的ClientApp類位於test包中,因此編譯器會在名爲test的當前目錄的子文件夾中查找它。您正在運行test文件夾中的javac,因此編譯器會在名爲test的子文件夾test中查找該類。這不是ClientApp類的地方,所以javac找不到它。

當您切換到父目錄並從那裏運行javac時,這兩個類都位於當前目錄test的子文件夾中,編譯器可以找到它們。

當我使用Java編譯器從test文件夾編譯代碼,我得到以下輸出:

C:\Users\Luke\Java\test>javac ServerApp.java 
ServerApp.java:40: error: cannot find symbol 
       ClientApp.finished = true; 
       ^
    symbol: variable ClientApp 
    location: class ServerApp.ServerThread 

    (5 similar errors omitted) 

這些可能是你所得到的同樣的錯誤。

如果我更改到父目錄,然後再試一次,你的代碼編譯好:

C:\Users\Luke\Java\test>cd .. 

C:\Users\Luke\Java>javac test/ServerApp.java 

C:\Users\Luke\Java> 
+0

它的工作!非常感謝! – 2014-09-03 20:17:08

相關問題