【发布时间】: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