URLClassLoader 在不同的目录中找不到 class

URLClassLoader can't find class thats in different directory

我正在制作 eclipse 插件,我需要从 eclipse 中的某个随机项目访问 selected class。我提取了 selected 文件:

IPath className;
Object firstElement, firstElement1;
/* Setting service to track when some object is selected in active workbench */
ISelectionService service = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getSelectionService();
IStructuredSelection structured = (IStructuredSelection) service.getSelection();
/* Getting first element of selected structure (path and workspace)*/
firstElement = structured.getFirstElement();
IResource resource = (IResource) Platform.getAdapterManager().getAdapter(firstElement, IResource.class);
resource.getProjectRelativePath();
 /* Extracting class name from selected object */
className = resource.getLocation();
String className1 = resource.getName();
className1 = FilenameUtils.removeExtension(className1);

在那之后,我想使用 URLClassLoader,这样我就可以获得 class 文件包含的内容。

URL url;
try {
    url = new URL("file:\d:\runtime-EclipseApplication\TestingProject\bin");
    URLClassLoader ucl = new URLClassLoader(new URL[] { url });
    Class<?> clazz = ucl.loadClass("org.eclipse.testing." + className1);
    Object o = clazz.newInstance();
} catch (Exception e){
    e.printStackTrace();
}

url 输出:"file:/d:/runtime-EclipseApplication/TestingProject/bin" 但是 ucl 不包含任何 classes,当我尝试加载 class "ucl.loadClass(className1)" 时,我得到了 exeption

**java.lang.ClassNotFoundException: org.eclipse.testing.Test**
**at java.net.URLClassLoader.findClass(Unknown Source)**
**at java.lang.ClassLoader.loadClass(Unknown Source)**
**at java.lang.ClassLoader.loadClass(Unknown Source)**
at org.plugin.generate_test.handlers.GenerateJUnitTest.execute(GenerateJUnitTest.java:58)
at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:291)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:92)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
**at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)**
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:305)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:239)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:152)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:494)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:487)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
at org.eclipse.e4.ui.workbench.renderers.swt.HandledContributionItem.executeItem(HandledContributionItem.java:431)
at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.handleWidgetSelection(AbstractContributionItem.java:446)
at org.eclipse.e4.ui.workbench.renderers.swt.AbstractContributionItem.lambda(AbstractContributionItem.java:472)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:86)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4428)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1079)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4238)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3817)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1150)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1039)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:153)
at org.eclipse.ui.internal.Workbench.lambda(Workbench.java:680)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:336)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:594)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:148)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:151)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:388)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:243)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
**at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)**
**at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)**
**at java.lang.reflect.Method.invoke(Unknown Source)**
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
at org.eclipse.equinox.launcher.Main.run(Main.java:1499)
at org.eclipse.equinox.launcher.Main.main(Main.java:1472)

他没有看到 Test.class 即使它在路径 d:\runtime-EclipseApplication\TestingProject\bin\ 和包 org.eclipse.testing 中 尝试将路径更改为 1. d:\runtime-EclipseApplication\TestingProject\bin\org\eclipse\testing 2. d:\runtime-EclipseApplication\TestingProject\bin\org\eclipse\testing\ 3. d:\runtime-EclipseApplication\TestingProject\bin\

也试过从 \ 切换到 /,文件:\\,文件:\, "classes = new URL("d:/runtime-EclipseApplication/TestingProject/bin/"); URL[] cp = {f.toURI().toURL()};"

"ucl.loadClass(className1);"

没有任何效果,它仍然没有在文件夹 bin 中看到任何 classes。也许我正在寻找项目 classes 的错误位置。 当我 运行 我的插件(开发中)像 Eclipse 应用程序时,我得到新的 Eclipse window 当我从某个项目 select class (与插件不同的位置) in), 菜单打开,当我 select 生成测试时,它应该让我在插件中得到 class 这样我就可以访问并列出它的方法,问题是我无法访问 class。我找到的唯一方法是使用 URLClassLoader,但是我被困住了,他看不到 class。有什么建议、提案、链接吗?谢谢

尝试以 / 结束你的路径。作为 URLClassLoader(URL[]) 的文档 说:

Any URL that ends with a / is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.

具体来说:

URL url;
try {
    url = new URL("file:///d:/runtime-EclipseApplication/TestingProject/bin/");
    URLClassLoader ucl = new URLClassLoader(new URL[] { url });
    Class<?> clazz = ucl.loadClass("org.eclipse.testing." + className1);
    Object o = clazz.newInstance();
} catch (Exception e){
    e.printStackTrace();
}