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