javaeclipseclassclassloaderurlclassloader

URLClassLoader can't find class thats in different directory


I'm making eclipse plug-in, and I need to access selected class from some random project in eclipse. I extracted selected file:

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

After that I wanted to use URLClassLoader so I can get classes that file contains.

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" but ucl doesn't contain any classes, and when I try loading class "ucl.loadClass(className1)" I get 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)

He doesn't see Test.class even tho it is on path d:\runtime-EclipseApplication\TestingProject\bin\ and in package org.eclipse.testing Tried changing path to 1. d:\runtime-EclipseApplication\TestingProject\bin\org\eclipse\testing 2. d:\runtime-EclipseApplication\TestingProject\bin\org\eclipse\testing\ 3. d:\runtime-EclipseApplication\TestingProject\bin\

Also tried switching from \ to /, file:\\, file:\, "classes = new URL("d:/runtime-EclipseApplication/TestingProject/bin/"); URL[] cp = {f.toURI().toURL()};"

"ucl.loadClass(className1);"

Nothing works, it still doesn't see any classes in folder bin. Maybe I'm looking at wrong place for project classes. When i run my plug-in (in development) like Eclipse Application, I get new Eclipse window where when I select class from some project (different location than plug-in), menu opens and when i select generate test, it is suppose to get me that class in plug-in so I can access and list it's methods, problem is I can't access class. Only way I found is using URLClassLoader, but yeah I got stuck with this, he can't see class. Any suggestions, proposals, links? thanks


Solution

  • Try ending your path with a /. As the docs for URLClassLoader(URL[]) say:

    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.

    Specifically:

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