title: How to use ClassLoader or Class to programatically load a Class
body: <p>I have two classes</p>
  
  <pre><code>package pack2;
  import java.lang.*;
  import java.io.*;
  class eg
  {
      public void show()
      {
          System.out.println("ClassPath set to this class");
      }
  }
  </code></pre>
  
  <p>this is in C:\NNK\pack2. the fully qualified name is pack2.eg</p>
  
  <p>another program is</p>
  
  <pre><code>import java.io.*;
  import java.lang.*;
  import java.net.*;
  class classload
  {
      public static void main(String args[])
      {
          //have to load the eg class here. Dont know what i have done below is right or wrong
          try 
          {
              Class b=Class.forName("pack2.eg");
          }
          catch(ClassNotFoundException e)
          {
              e.printStackTrace();
          }
          try
          {
              eg z=(eg) b.newInstance();
          }
          catch(InstantiationException l)
          {
              l.printStackTrace();
          }
          z.show();
          System.out.println("b.getName()="+b.getName());
      }
  }
  </code></pre>
  
  <p>this program is at C:\NNK
  I have to load the eg program here. I tried to learn it in Oracle saw other related stack overflow questions of it. 
  <a href="https://stackoverflow.com/questions/5571466/dynamically-loading-a-class-in-java">Dynamically loading a class in Java</a>
  But it didn't work i keep getting the error b is Unknown and z is unknown symbol.
  Also is there a way to use the directory filename(eg: C:\NNK\pack2\eg) to load a class</p>