title: How to get a package name of .class file?
body: <p>I have an unusual problem which is concerned to dynamic loading java .class file at run-time. All I want to do is to load a .class file and basing on it create a Class object.</p>
  
  <p>Input: an absolute path of .class file. </p>
  
  <p>Basing on it i want to load class by ClassLoader, so I need a path of root directory where file is located and full class name e.g com.test.MyClass. Basing on mentioned absolute path I can only get a class name but I can't get a package name which is "hiden" in this file.</p>
  
  <p>Here is code of my "loading class method": </p>
  
  <pre><code>    public static void loadClass(String directory){
  
          // Get file root directory
          String rootDirectory = new File(directory).getParent();
  
          // Get rid of file extension
          String className = getFileNameWithoutExtension(directory);
  
          URL[] urls = null;
          ClassLoader cl = null;
  
          try {
              // Convert File to a URL and save them
              urls = new URL[]{new File(rootDirectory).toURI().toURL()};
  
              // Create a new class loader with the directory
              cl = new URLClassLoader(urls);
  
              // Load in the class
              dynamicClass = cl.loadClass(className);
          } 
          catch (MalformedURLException e) 
          {
  
      } 
      catch (ClassNotFoundException e) 
      {
  
      }
      catch (NoClassDefFoundError e)
      {
          // Basing on error message get the class package name
          String classPackage = getClassPackage(e.getMessage());
  
          try {             
  
             // Load the class once more!
             dynamicClass = cl.loadClass(classPackage);
          } 
          catch (ClassNotFoundException ex) 
          {
  
          }
      }
  }
  </code></pre>
  
  <p>Second method is used to get package name from exception message:</p>
  
  <pre><code>private static String getClassPackage(String errorMsg){
  
      // Start and end index of cutting
      int startIndex = errorMsg.lastIndexOf(" ") + 1;
      int endIndex = errorMsg.length() - 1;
  
      // Let's save a substring
      String classPackage = errorMsg.substring(startIndex, endIndex);
  
      // Replace char '/' to '.'
      classPackage = classPackage.replace('/', '.');
  
      return classPackage;
  }
  </code></pre>
  
  <p>Code of method <strong>getFileNameWithoutExtension</strong>:</p>
  
  <pre><code>private static String getFileNameWithoutExtension(String path){
      int start = path.lastIndexOf(File.separator) + 1;
      int end = path.lastIndexOf(DOT);
      end = start &lt; end ? end : path.length();
      String name = path.substring(start, end);
  
      return name;
  }
  </code></pre>
  
  <p>Where the static final variable is:</p>
  
  <pre><code>private static final String DOT = ".";
  </code></pre>
  
  <p>And here is my question: is it possible to get package name from .class file without using this kind of trick?</p>