当我用eclipse运行我的java应用程序时,我没有遇到任何问题,但是当我使用命令提示符运行它时,我得到NoClassDefFoundError。
C:\Windows\System32>cd C:\Users\Caco\workspace\Bisquit_server\bin\bisquit_server
C:\Users\Caco\workspace\Bisquit_server\bin\bisquit_server>java ReceiveMsg
Exception in thread "main" java.lang.NoClassDefFoundError: ReceiveMsg (wrong nam
e: bisquit_server/ReceiveMsg)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
C:\Users\Caco\workspace\Bisquit_server\bin\bisquit_server>
这是我的java代码:
package bisquit_server;
import java.io.*;
import java.net.*;
public class ReceiveMsg {
private ServerSocket server_socket;
private Socket socket;
ObjectInputStream input_stream;
private static String msg;
public ReceiveMsg() throws IOException, ClassNotFoundException{
try
{
server_socket=new ServerSocket(50000,1);
while(true){
listen();
System.out.print("Server is ready to connect to a client");
createStreams();
initProcessing();
}
}
finally {close();}
}
private void listen() throws IOException{
socket= server_socket.accept();
ReceiveMsgTh rmth = new ReceiveMsgTh();
Thread t = new Thread(rmth);
}
private void createStreams() throws IOException{
input_stream= new ObjectInputStream(socket.getInputStream());
}
private void initProcessing() throws ClassNotFoundException, IOException{
msg = "";
msg = (String)input_stream.readObject();
}
private void close() throws IOException{
if (input_stream!=null && socket != null){
input_stream.close();
socket.close();
}
}
public static void main(String[] args) //throws ClassNotFoundException, IOException
{
try {
new ReceiveMsg();
}
catch(ClassNotFoundException | IOException e){}
new Store().StoreMsg(msg,new String[]{"0987654321,"1234567890"},"1234567890");
}
}
package bisquit_server;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class ReceiveMsgTh implements Runnable {
private ServerSocket server_socket;
private Socket socket;
ObjectInputStream input_stream;
public void StartProcess() throws IOException, ClassNotFoundException{
try
{
server_socket=new ServerSocket(50000,1);
while(true){
listen();
createStreams();
initProcessing();
}
}
finally {close();}
}
private void listen() throws IOException{
socket= server_socket.accept();
}
private void createStreams() throws IOException{
input_stream= new ObjectInputStream(socket.getInputStream());
}
private void initProcessing() throws ClassNotFoundException, IOException{
String msg = "";
msg = (String)input_stream.readObject();
new Store().StoreMsg(msg,new String[]{"0987654321","1234567890"},"1234567890");
}
private void close() throws IOException{
if (input_stream!=null && socket != null){
input_stream.close();
socket.close();
}
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
StartProcess();
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
}
}
}
package bisquit_server;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Store {
public void StoreMsg(String msg, String[] mobileNumbers, String whoWrite){
File f = new File("C:\\Users\\Caco\\workspace\\Conversations\\"+mobileNumbers[0]+mobileNumbers[1]+".txt");
try (
BufferedWriter bw = new BufferedWriter(new FileWriter(f,true));
)
{
bw.write(whoWrite);
bw.write(" ");
bw.write(msg);
bw.write("\r\n");
}
catch(IOException e) {
System.out.print("Error writing file");
}
}
}
我真的很沮丧,因为我试着修复它而没有成功的三天! 你能帮助我吗? 三江源
答案 0 :(得分:2)
只需
cd ..
java bisquit_server.ReceiveMsg
请记住,您不使用java命令执行文件,而是类。您需要为java命令提供完全限定的类名。
此外,它可以找到包含该类的类文件。这是通过所谓的类路径完成的,当你不给出时,它只是当前目录。因此,要查找bisquit_server.ReceiveMsg
,java将在类路径和目录中查找目录bisquit_server/
,它将查找ReceiveMsg.class
文件。
这样,您就可以从其他位置运行程序:
cd /temp
java -cp C:\Users\Caco\workspace\Bisquit_server\bin bisquit_server.ReceiveMsg
答案 1 :(得分:0)
首先运行javac
命令以创建.class
个文件。之后用java命令运行
javac ReceiveMsg.java
之后
cd..
java bisquit_server.ReceiveMsg