我正在學習在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();
}
}
有人能幫助我嗎?謝謝。
檢查,如果您使用的是保存版本的編譯和運行你的代碼。 – Hannes 2014-09-03 18:53:15
是的,他們都是1.6.0_65版本。 – 2014-09-03 19:20:18
該編譯器輸出錯誤!你期望什麼? – chrylis 2014-09-03 19:51:18