Team,
I am struck with a basic error for quite sometime. Can someone pin point what i am missing here.
Created a simple java program (which would connect to a socket and send a message). Code mentioned below
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);
}
}
}
Compliling and running with IDE - Successful.
If tried with Command prompt, it fails with below exception.
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.
Tried setting classpath also. The .class file is also located in the same path as .java file.
The steps followed in Command prompt is below for reference.
Step 1
C:\Users\prem\Documents\NetBeansProjects\Client Sever Socket Example\src\ ent\sever\socket\example>set Path="C:\Program Files\Java\jdk1.6.0_32\bin"
Step 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"
Step 3
C:\Users\pre00185\Documents\NetBeansProjects\Client Sever Socket Example\src\ ent\sever\socket\example>javac Clientprog.java
Step 4 C:\Users\pre00185\Documents\NetBeansProjects\Client Sever Socket Example\src\ ent\sever\socket\example>java Clientprog
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.