队
很长一段时间我都遇到了一个基本的错误。有人可以指出我在这里缺少的东西。
创建一个简单的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 Sever Socket示例\ src \ ent \ sever \ socket \ example&gt;设置Path =“C:\ Program Files \ Java \ jdk1.6.0_32 \ bin”
第2步 C:\ Users \ prem \ Documents \ NetBeansProjects \ Client Sever Socket示例\ src \ ent \ sever \ socket \ example&gt;设置HomePath =“C:\ Program Files \ Java \ jdk1.6.0_32”
第3步
C:\ Users \ pre00185 \ Documents \ NetBeansProjects \ Client Sever Socket示例\ src \ ent \ sever \ socket \ example&gt; javac Clientprog.java
第4步 C:\ Users \ pre00185 \ Documents \ NetBeansProjects \ Client Sever Socket示例\ src \ ent \ sever \ socket \ example&gt; java Clientprog
线程“main”中的异常java.lang.NoClassDefFoundError:Clientprog 引起:java.lang.ClassNotFoundException:Clientprog 在java.net.URLClassLoader $ 1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) 在java.net.URLClassLoader.findClass(URLClassLoader.java:190) at java.lang.ClassLoader.loadClass(ClassLoader.java:306) 在sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:247) 找不到主类:Clientprog。程序将退出。
答案 0 :(得分:0)
您需要在运行java命令时指定类路径,并使用其完全限定的类名引用您的类。
SET CP="C:\Users\pre00185\Documents\NetBeansProjects\Client Sever Socket Example\src"
java -cp %CP% client.sever.socket.example.Clientprog
这假定您的类文件与源位于同一位置。根据需要进行调整。