Java load plugin jar ClassNotFoundException -
I'm new here and sorry for my bad english;)
I try to load the plugin jar in Java with this code:
package testing program; Import java.io.file; Import java.net.URL; Import java.net.URLClassLoader; Import java.util.HashSet; Import java.util.Set; Public Class Training Program {Private Stable Set & lt; Plugin & gt; Plugins = New Hashet & lt; Plugin & gt; (); Private stable test program example = new test program (); Public static zero main (string [] args) {System.out.println ("test program v0.1"); File plugins directory = new file ("plugins"); If (pluginsDirectory.exists () and pluginsDirectory.isDirectory ()) {file plugin file: plugins directory .list files ()) {if plugin plugin.exists () & pluginfile.implex () & amp; PluginFile .getName (). EndsWith (".jar")) {try {URL} URL = New URL [] {pluginFile.toURI () ToURL ()}; Class loader loader = new url class loader (url); Class c = loader.loadClass ("src." + PluginFile.getName () .toLowerCase (). Change (".jar", "") "". "");; Object plugin object = c. Ny instance () ; PluginObject instance plugin; Plugin plugin plugin Plugin.add plugin (plugin); plugin.onEnable ();}} hold (exception e) {e.printStackTrace (); }}}} While {true} {if (true) {// code break}}} (plugin plugin: plugin) {plugin.onDisable ();}} public int getInt () {return 5;}} < / Code>
Plugin class:
package testprogramm; Public intangible class plugin {Protected TestProogramm TestProgramm; protected void Init (test program program exam program) {this.testProgramm = testProgramm;} public test program getTestProgramm () {return test programm;} enabled on public essence zero; disabled on public abstract void; event 1 Public essence zero (string event); public essence zero (string event) on event 2; public essence zero (string event) on event 3; }
Classroom in the plugin jar:
package self-plugins; Importprogramm.Plugin Import; Public category main plugin {@Override enabled on Pure Zero () (System.out.println ("OwnPlugin Enabled");} @ Override Disabled at Public Zero () {System.out.println ("Disable Plugin Automatically"); } @ Override Public Wide On Avent 1 (String Array 0) {} @ Override Public Wide On Evening 2 (String Array 0) {} @ Override Public Video on Entente 3 (String AZR) {}}
< P> I get this class notfound exception: java.lang.ClassNotFoundException: at java.net.URL Classic Loader $ 1.run (unknown source) .java.net.URLClassLoader $ 1.run (Unknown Source) on Java.lang.ClassLoader.loadClass on java.lang.ClassLoader.loadClass (unknown source) on testprogramm.TestProgramm (Unknown source) .main (TestProgramm.java:26)
What can I do? What's wrong, please help me.
You feel that trying to use a class named src.ownplugin is the main
. However, your main
class is actually called ownplugin.main
. Try changing it
class c = loader.loadClass ("src." + PluginFile.getName (). Replace ToLowerCase (). (".jar", "") + " .Main" );
with this
class c = loader.loadClass (pluginFile.getName () .toLowerCase (). Replace (".jar", " ") +" Main ");
Comments
Post a Comment