-1

when i run my java application with eclipse i don't get any problem but when i run it with the command prompt i get 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>

this is my java code:

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");
        }


}

}

I'm really frustrated because it's three days that i try to fix it without succes! Can you help me? Thankyou

Grant Birchmeier
  • 17,809
  • 11
  • 63
  • 98
Caco85
  • 23
  • 6

2 Answers2

2

Just

cd ..
java bisquit_server.ReceiveMsg

Remember, you don't execute files with the java command, but classes. You need to give the fully qualified class name to the java command.

Also, it mmust be possible to find the class file that contains the class. This is done via the so-called class-path, which is just the current directory when you don't give one. So, to find bisquit_server.ReceiveMsgjava will look up a directory bisquit_server/ in the class path and in that directory, it will look for the ReceiveMsg.class file.

This way, you can run your program from a different location:

cd /temp
java -cp C:\Users\Caco\workspace\Bisquit_server\bin  bisquit_server.ReceiveMsg
Ingo
  • 36,037
  • 5
  • 53
  • 100
0

run javac command first to create .class files. after that run with java command

javac ReceiveMsg.java

and after that

cd..
java bisquit_server.ReceiveMsg
lakshman
  • 2,641
  • 6
  • 37
  • 63