-2

团队,

很长一段时间以来,我一直对一个基本错误感到震惊。有人可以指出我在这里缺少什么。

创建了一个简单的 java 程序(它将连接到套接字并发送消息)。下面提到的代码

package client.sever.socket.example;

import java.io.*;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.*;

/**
 *
 * @author prem
 */
public class Clientprog {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        /** Define a host server */
                String host = "localhost";
    /** Define a port */
                int port = 19997;

    StringBuffer instr = new StringBuffer();
    String TimeStamp;
    System.out.println("SocketClient initialized");

    try {
      /** Obtain an address object of the server */
      InetAddress address = InetAddress.getByName(host);
      /** Establish a socket connection */
      Socket connection = new Socket(address, port);
      /** Instantiate a BufferedOutputStream object */

       BufferedOutputStream bos = new BufferedOutputStream(connection.
          getOutputStream());

      /** Instantiate an OutputStreamWriter object with the optional character
       * encoding.
       */
      OutputStreamWriter osw = new OutputStreamWriter(bos, "US-ASCII");

      TimeStamp = new java.util.Date().toString();
      /*String process = "Calling the Socket Server on "+ host + " port " + port +
          " at " + TimeStamp +  (char) 13;*/
      String process = "01234"+  (char) 13;

      /** Write across the socket connection and flush the buffer */
      osw.write(process);
      osw.flush();

      /** Instantiate a BufferedInputStream object for reading
      /** Instantiate a BufferedInputStream object for reading
       * incoming socket streams.
       */

      BufferedInputStream bis = new BufferedInputStream(connection.
          getInputStream());
      /**Instantiate an InputStreamReader with the optional
       * character encoding.
       */

      InputStreamReader isr = new InputStreamReader(bis, "US-ASCII");

      /**Read the socket's InputStream and append to a StringBuffer */
      int c,d=0;
      try
      {
          if(isr.ready())
          {
          }
          else
              System.out.println("NO RESPONSE");
      while (( (c = isr.read()) != 13) && d<4)
      {
        instr.append( (char) c);
        System.out.println("Inside Loop - "+instr);
        d++;
      }}
      catch(OutOfMemoryError e)
      {
          System.out.println("OOM ERROR - "+e);
          System.out.println(instr);
      }
      /** Close the socket connection. */
      connection.close();
      System.out.println(instr);

    }
    catch(UnknownHostException e)
    {
     System.out.println("Unknown Host Exception: " + e);
    }
    catch(ConnectException e)
    {
     System.out.println("ConnectException: " + e);
    }
    catch(Exception e1)
    {
     System.out.println("Exception: " + e1);
    }
    }
}

使用 IDE 编译和运行 - 成功。

如果尝试使用命令提示符,则会失败并出现以下异常。

Exception in thread "main" java.lang.NoClassDefFoundError: Clientprog
Caused by: java.lang.ClassNotFoundException: Clientprog
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: Clientprog.  Program will exit.

也尝试设置类路径。.class 文件也位于与 .java 文件相同的路径中。

下面是命令提示符中遵循的步骤以供参考。

步骤1

C:\Users\prem\Documents\NetBeansProjects\Client 服务器套接字示例\src\ ent\sever\socket\example>set Path="C:\Program Files\Java\jdk1.6.0_32\bin"

步骤 2 C:\Users\prem\Documents\NetBeansProjects\Client Sever Socket Example\src\ ent\sever\socket\example>set HomePath="C:\Program Files\Java\jdk1.6.0_32"

第 3 步

C:\Users\pre00185\Documents\NetBeansProjects\Client 服务器套接字示例\src\ ent\sever\socket\example>javac Clientprog.java

步骤 4 C:\Users\pre00185\Documents\NetBeansProjects\Client Sever Socket Example\src\ ent\sever\socket\example>java Clientprog

线程“main”中的异常 java.lang.NoClassDefFoundError: Clientprog 原因:java.lang.ClassNotFoundException: Clientprog at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method ) 在 java.net.URLClassLoader.findClass(URLClassLoader.java:190) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:306) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 在 java .lang.ClassLoader.loadClass(ClassLoader.java:247) 找不到主类:Clientprog。程序将会退出。

4

1 回答 1

0

您需要在运行 java 命令时指定类路径,并使用其完全限定的类名引用您的类。

SET CP="C:\Users\pre00185\Documents\NetBeansProjects\Client Sever Socket Example\src"
java -cp %CP% client.sever.socket.example.Clientprog

这假设您的类文件与源文件位于同一位置。根据需要进行调整。

于 2015-02-24T19:55:24.570 回答