【问题标题】:URLClassLoader can't find class thats in different directoryURLClassLoader 在不同目录中找不到类
【发布时间】:2018-09-28 16:21:35
【问题描述】:

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

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 来获取文件包含的类。

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 不包含任何类,当我尝试加载类“ucl.loadClass(className1)”时,我得到了异常

**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$2(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$5.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$3(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)

即使它位于路径 d:\runtime-EclipseApplication\TestingProject\bin\ 和包 org.eclipse.testing 中,他也没有看到 Test.class 尝试更改路径 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 中的任何类。也许我正在寻找项目课程的错误位置。 当我像 Eclipse 应用程序一样运行我的插件(在开发中)时,我得到新的 Eclipse 窗口,当我从某个项目(与插件不同的位置)中选择类时,菜单打开,当我选择生成测试时,假设在插件中给我那个类,这样我就可以访问并列出它的方法,问题是我无法访问类。我发现的唯一方法是使用 URLClassLoader,但是我被困住了,他看不到课程。有什么建议、建议、链接吗?谢谢

【问题讨论】:

  • 容易记住的规则:在所有平台上,URL总是使用正斜杠 (/)。

标签: java eclipse class classloader urlclassloader


【解决方案1】:

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

任何以/ 结尾的 URL 都被假定为指向一个目录。否则,假定 URL 引用一个 JAR 文件,该文件将根据需要下载和打开。

具体来说:

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();
}

【讨论】:

  • 我应该把 / 放在 bin 文件夹之后,还是应该包含像 bin\\org\\eclipse\\testing/ 这样的包,我试过这样 url = new URL("file:\\d :\\runtime-EclipseApplication\\TestingProject\\bin\\org\\eclipse\\testing/");现在我得到: java.lang.NoClassDefFoundError: Test (wrong name: org/eclipse/testing/Test)
  • 已编辑。试试我的例子。
  • 是的,它有效,谢谢。我不知道为什么它会丢失 try/catch 块中的所有变量值,即使我在块之前声明它们也是如此。但是谢谢你:)
猜你喜欢
  • 2012-07-16
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-04
  • 2019-07-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多