简体   繁体   中英

NoClassDefFound when running from cmd but not Eclipse

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?

You need to specify the full name of your class (including package).

java com.mycompany.pdr.client.SimpleClientSend

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:

java -cp ...MyWorkspace/myProject com.mycompany.pdr.client.SimpleClientSend

The ... of course is since I don't know your full path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM