title: Load Class, Generate Object of this class
body: <p>I'm a beginner at java Reflection/Dynamic Loading. I want to load a specific Class ( that implements an Interface ) from a jar-File. Further more i want to initialize an Object of this Class and cast it to my specific Interface.</p>
  
  <p>I tried <code>.loadClass()</code>, <code>class.forName()</code> and if I cast <code>Myclass.newInstance</code> to my Interface I'll get a ClassCastException.</p>
  
  <p>How could I program my plugin-Loader, so it loads my class in my jar and cast it to <code>myInterface</code>?</p>
  
  <h2>Edit</h2>
  
  <p>My Server has the same Interface as my Plugins implement. Now I want my Plugins "loaded" and cast to my Interface so I could use them normally.</p>
  
  <h2>Edit</h2>
  
  <p>Here is some sample Code of my try to get a rid of these errors :D</p>
  
  <pre><code>URL url = f.toURL();  
  URL[] urls = new URL[]{url};
  ClassLoader cl = new URLClassLoader(urls);
  
  Class&lt;ModInterface&gt; c = (Class&lt;ModInterface&gt;)cl.loadClass("com.myapp.mod");
  
  Class&lt;? extends ModInterface&gt; sub = c.asSubclass(ModInterface.class);
  Constructor&lt;? extends ModInterface&gt; con = sub.getConstructor();
  ModInterface mi = con.newInstance();
  </code></pre>
  
  <p>another code sample what I've done:</p>
  
  <pre><code>               ClassLoader loader = PluginLoader.class.getClassLoader();
                 URL url = new URL("jar:file:" + f.getAbsolutePath() + "!/");
                 URLClassLoader ucl = new URLClassLoader(new URL[] { url }, loader);
                 Class iInterfaceClass = Class.forName("com.myapp.mod", true, ucl);
                 ModInterface mi = (ModInterface) iInterfaceClass.newInstance();
  </code></pre>
  
  <h2>Error:</h2>
  
  <pre><code>java.lang.ClassCastException: com.myapp.mod cannot be cast to myapp.ModInterface
  </code></pre>
  
  <p>After some help provided by you i've done the following:</p>
  
  <pre><code>URL url = f.toURI().toURL();
     URL[] urls = new URL[]{url};
     ClassLoader loader = ModInterface.class.getClassLoader();
     ClassLoader cl = new URLClassLoader(urls, loader);
     Class&lt;?&gt; loadedClass = Class.forName("com.myapp.mod", true, cl);
     Class&lt;? extends ModInterface&gt; pluginClass = loadedClass.asSubclass(ModInterface.class);
     ModInterface mi = pluginClass.newInstance();
  </code></pre>
  
  <p>Now i get this error -> java.lang.ClassCastException: class com.myapp.mod thrown at loadedClass.asSubclass(..);
  The specific class is found, because if i change com.myapp.mod to whatever it throws a classnotfound execption. I've tried to use the ModInterface ClassLoader, but nothin changed. with(out) i'll get the same error.</p>
  
  <p>Note://
  I use 2 Projects. 1 for my Server App, the other for my Plugin that should be loaded. Both have the same Interface.</p>
  
  <p>edit://</p>
  
  <p>with this code i get an Object of type Object from my desired class, but it's not an instance of my ModInterface although my Class implements my ModInterface. If i try to cast my Object to my ModInterface a Error appears. Here is my short code:</p>
  
  <pre><code>Class c = clazzLoader.loadClass(element.getName().replaceAll(".class", "").replaceAll("/", "."));
  Object instance = c.newInstance();
  </code></pre>