私は単純なプラグイン アーキテクチャを実装する必要がある大学の Java プロジェクトに取り組んでいます。プロジェクト A のメイン アプリケーションは、別のプロジェクト B にある指定されたディレクトリからプラグインをロードする必要があります。このトピックに関する調査 (stackoverflow を含む) を行った後、URLClassLoader を使用してクラスをロードすることにしました。プロジェクト B はプロジェクト A を参照し、すべてのプラグインは共通のプラグイン クラスを拡張します (インターフェイスである可能性もありますが、違いはありません)。これは私がこれまでに得たものです:
ロードしようとするプラグイン クラスのソース:
package plugin;
import de.uks.student.pluginpattern.model.EditorPlugin;
public class Circle extends EditorPlugin
{
@Override
public void init()
{
}
}
クラスをロードするはずのコード:
public void init(String[] args)
{
editorPane = new EditorPane().withWidth(500).withHeight(500);
toolBar = new ToolBar();
plugins = new EditorPluginSet();
// load plugins
File pluginDir = new File(PLUGIN_PATH);
if (!pluginDir.exists())
{
System.err.println("Plugin path not found!!");
return;
}
String[] plugins = pluginDir.list();
if (plugins == null)
{
System.err.println("Plugin path points to a file!!");
return;
}
for (String pluginString : plugins)
{
for (String argString : args)
{
if (pluginString.contains(argString))
{
System.out.println("Loading plugin from " + pluginString);
File pluginFile = new File(PLUGIN_PATH + "/");
// as getAbsolutePath embeds the relative path ... /../pluginProject ...
// which may not be processed correctly by the OS (Windows at least),
// we correct the path manually (just to be on the safe side)
String absolutePath = pluginFile.getAbsolutePath();
String[] split = absolutePath.split("\\\\");
List<String> splitsimpleList = Arrays.asList(split);
ArrayList<String> splitList = new ArrayList<>(splitsimpleList);
for (int i = 0; i < splitList.size() - 1; ++i)
{
if (splitList.get(i + 1).equals(".."))
{
splitList.remove(i);
splitList.remove(i);
break;
}
}
StringBuilder b = new StringBuilder();
for (String string : splitList)
{
b.append(string);
b.append("/");
}
File pluginFileForRealThisTime = new File(b.toString());
URL pluginURL = null;
try
{
pluginURL = pluginFileForRealThisTime.toURI().toURL();
} catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
URL[] urls = {pluginURL};
ClassLoader parentClassLoader = this.getClass().getClassLoader();
URLClassLoader uLoader = new URLClassLoader(urls, parentClassLoader);
Class<?> pluginClass = null;
try
{
String className = "plugin." + argString;
pluginClass = uLoader.loadClass(className);
} catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
今、私は常に ClassNotFoundException で終わります:
java.lang.ClassNotFoundException: plugin.Circle at java.net.URLClassLoader$1.run(Unknown Source) at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java. de.uks.student.pluginpattern.model.EditorSystem.init(EditorSystem. java:412) de.uks.student.pluginpattern.EditorSystem.main(EditorSystem.java:13) で
私はすでにそれをチェックしました
- 生成された URL は、任意の Web ブラウザーと Windows エクスプローラーで正しく解決できます。
- Circle.class がビルドされ、使用された URL によって参照されるディレクトリに存在します
- className は「plugin.Circle」に解決されます。これは、使用する正しいバイナリ名である必要があります
- plugin.Circle から継承を削除しても違いはありません。
他に何を試すかについてのアイデアがほとんどなくなりました。私は何を間違っていますか?