从 cmd 运行但不是 Eclipse 时出现 NoClassDefFound

发布于 2024-12-21 11:49:33 字数 2343 浏览 0 评论 0原文

我知道当我弄清楚这个问题时我会感觉自己像个迟钝者。我正在尝试做一个非常简单的客户端/服务器并从命令行运行它。它在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

淑女气质 2024-12-28 11:49:33

您需要指定类的全名(包括包)。

java com.mycompany.pdr.client.SimpleClientSend

类的结构基础包含在类路径中非常重要。通常情况下 。包含在内,因此如果您在 myProject 中运行上面的命令,它应该可以工作。

如果您位于另一个文件夹中,您应该将 myProject 添加到您的类路径中,例如:

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

... 当然是因为我不知道您的完整路径。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文