我知道当我想出这个时,我会觉得自己像个傻瓜。我正在尝试做一个非常简单的客户端/服务器并从命令行运行它。它从Eclipse运行良好,但不是从cmd运行。这是客户:
package com.mycompany.pdr.client;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;
public class SimpleClientSend {
public static void main(String[] args) {
String host = "127.0.0.1";
int port = 11048;
String dataToSend = "HELLO SERVER";
System.out.println("> Trying to connect...");
System.out.println("> Opening connection to server [" + host + ":"
+ port + "]...");
Socket socket1Connection;
try {
socket1Connection = new Socket(host, port);
System.out.println("> Connected...");
System.out.println("> Trying to write data... [ " + dataToSend + " ]");
BufferedOutputStream bos = new BufferedOutputStream(
socket1Connection.getOutputStream());
/*
* Instantiate an OutputStreamWriter object with the optional
* character encoding. e.g. UTF-8
*/
OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");
// Writing to server
osw.write(dataToSend);
osw.flush();
System.out.println("> Writing to server done...");
socket1Connection.close();
} catch (UnknownHostException e) {
System.out.println(e.getLocalizedMessage());
System.out.println("Unknown Host. Please check if the server is running at the IP & port");
//e.printStackTrace();
} catch (IOException e) {
System.out.println(e.getLocalizedMessage());
System.out.println("Could not send data. Giving up.");
//e.printStackTrace();
}
System.out.println("> End of connection...");
}
}
我的目录结构是:(MyWorkspace)/myProject/com/mycompany/pdr/client
我从客户端文件夹中运行javac SimpleClientSend.java
,我得到一个类文件,没有错误。我运行java SimpleClientSend
并收到NoClassDefFound消息:
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleClientSend (wrong name: com/iai/pdr/client/SimpleClientSend)
当我运行java时,我尝试使用-cp .
来跟踪其他所有文章的建议(但是,如果。已经在我的类路径中,那又有什么意义?),我试过从外面运行它客户端文件夹,一切只是给我同样的错误。在eclipse中,我所要做的就是将java文件粘贴到一个空白项目中然后运行。我在这里做错了什么?
答案 0 :(得分:3)
您需要指定班级的全名(包括包)。
java com.mycompany.pdr.client.SimpleClientSend
重要的是,类的结构基础包含在类路径中。一般 。包含,所以如果你在myProject中运行上面的命令它应该工作。
如果您在另一个文件夹中,则应将myProject添加到类路径中,例如:
java -cp ... MyWorkspace / myProject com.mycompany.pdr.client.SimpleClientSend
......当然是因为我不知道你的完整路径。