从 cmd 运行但不是 Eclipse 时出现 NoClassDefFound
我知道当我弄清楚这个问题时我会感觉自己像个迟钝者。我正在尝试做一个非常简单的客户端/服务器并从命令行运行它。它在 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 文件粘贴到一个空白项目中,然后它就运行了。我在这里做错了什么?
I know I'll feel like a tard when I figure this one out. I'm trying to do a very simple client/server and run it from the command line. It runs fine from Eclipse, but not from cmd. Here's the client:
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...");
}
}
My directory structure is : (MyWorkspace)/myProject/com/mycompany/pdr/client
I run javac SimpleClientSend.java
from inside the client folder, and I get a class file, no errors. I run java SimpleClientSend
and I get a NoClassDefFound message:
Exception in thread "main" java.lang.NoClassDefFoundError: SimpleClientSend (wrong name: com/iai/pdr/client/SimpleClientSend)
I've tried using -cp .
when I run java to follow the suggestions of every other article out there(but what's the point if . is already in my classpath?), I've tried running it from outside the client folder, everything just gives me the same error. In eclipse, all I had to do was paste the java files into a blank project and it ran. What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要指定类的全名(包括包)。
类的结构基础包含在类路径中非常重要。通常情况下 。包含在内,因此如果您在 myProject 中运行上面的命令,它应该可以工作。
如果您位于另一个文件夹中,您应该将 myProject 添加到您的类路径中,例如:
... 当然是因为我不知道您的完整路径。
You need to specify the full name of your class (including package).
It is important that the base of the structure for your class(es) is included in your class path. Normally . is included, so if you run the command above when your are in myProject it should work.
If you are in another folder you should add the myProject to your class path, such as:
The ... of course is since I don't know your full path.