How do you load a class that is not in the classpath & is in a jar? : <p>I want to load a class that is in a jar.</p>

<p>I already tried to just load the class using this code:</p>

<pre class="lang-java prettyprint-override"><code>URL dirUrl = jarFile.toURL();
URLClassLoader ucl = new URLClassLoader(new URL[] {dirUrl}, getClass().getClassLoader());
Class&lt;?&gt; clazz = ucl.loadClass(mainClass);
ucl.close();
</code></pre>

<p>I get an exception that it couldn´t find the class:</p>

<pre class="lang-py prettyprint-override"><code>java.lang.RuntimeException: java.lang.ClassNotFoundException: net.api.main
        at net.al.Main.onEnable(Main.java:39) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_211]
Caused by: java.lang.ClassNotFoundException: net.api.main
        at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_211]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_211]
        at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_211]
        at net.al.addon.AddonLoader.loadAddons(AddonLoader.java:129) ~[?:?]
        at net.al.Main.onEnable(Main.java:36) ~[?:?]
        ... 16 more
</code></pre>
