-2

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.

Prem
  • 4,823
  • 4
  • 31
  • 63
  • I referrred to many links and other SO questions similar to this, but still the issue is not resolved. So, i have posted this question and steps i followed. – Prem Feb 24 '15 at 19:53
  • Try setting the class search path on the command line to include the current working directory also known as "."; e.g.: java -cp . Clientprog – chrisinmtown Feb 24 '15 at 19:55
  • javac client/sever/socket/example/ClientProg.java in src path and java client.sever.socket.example.ClientProg – anurag gupta Feb 24 '15 at 19:56
  • @anuraggupta, i dont understand what you are trying to say here – Prem Feb 24 '15 at 19:58
  • @chrislott, i tried that option. It did not work – Prem Feb 24 '15 at 19:58
  • i mean that; to run go to the location "C:\Users\pre00185\Documents\NetBeansProjects\Client Sever Socket Example\src" and then hit "java client.sever.socket.example.ClientProg" after compilation and it'll work – anurag gupta Feb 24 '15 at 20:04
  • @anuraggupta, I tried. It is still the same. The .class file of Clientprog is in the same path as that as .java file. But still i am getting this error. – Prem Feb 24 '15 at 20:23

1 Answers1

0

You need to specify the classpath when running the java command as well as reference you class with its fully qualified class name.

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

This assumes your class file is in the same location as your source. Adjust this as needed.

Brent Worden
  • 10,624
  • 7
  • 52
  • 57
  • I tried. The .class file of Clientprog is in the same path as that as .java file. But still i am getting this error. – Prem Feb 24 '15 at 20:23