我正在制作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 output:“file:/ d:/ runtime-EclipseApplication / TestingProject / bin” 但是ucl不包含任何类,当我尝试加载类“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$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)
他甚至没有看到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 \
还尝试从\切换到/,file:\\,file:\, “classes = new URL(”d:/ runtime-EclipseApplication / TestingProject / bin /“); URL [] cp = {f.toURI()。toURL()};“
“ucl.loadClass(className1);”
没有任何作用,它仍然没有在文件夹bin中看到任何类。也许我正在寻找项目类错误的地方。 当我运行我的插件(在开发中)像Eclipse Application时,我得到了新的Eclipse窗口,当我从某个项目(插件不同的位置)选择类时,菜单打开,当我选择生成测试时,我想让我在插件中的类,所以我可以访问并列出它的方法,问题是我无法访问类。我找到的唯一方法是使用URLClassLoader,但是我坚持这个,他不能看到课程。有什么建议,建议,链接?感谢
答案 0 :(得分:0)
尝试使用/
结束路径。作为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();
}