Java URLClassLoader找不到位于不同目录中的类

Java URLClassLoader找不到位于不同目录中的类,java,eclipse,class,classloader,urlclassloader,Java,Eclipse,Class,Classloader,Urlclassloader,我正在制作eclipse插件,需要从eclipse中的某个随机项目中访问选定的类。我提取了所选文件: IPath className; Object firstElement, firstElement1; /* Setting service to track when some object is selected in active workbench */ ISelectionService service = PlatformUI.getWorkbench().getActiveWor

我正在制作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();
}
即使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 URLd:/runtime EclipseApplication/TestingProject/bin/; URL[]cp={f.toURI.toURL}

ucl.loadClassName1

什么都不起作用,它仍然看不到文件夹箱中的任何类。也许我找错了项目课的位置。
当我运行像Eclipse应用程序这样的插件开发时,我会得到一个新的Eclipse窗口,在这个窗口中,当我从与插件不同的项目位置选择类时,菜单会打开,当我选择generate test时,它会在插件中给我那个类,这样我就可以访问并列出它的方法,问题是我无法访问类。我找到的唯一方法是使用URLClassLoader,但我被这个卡住了,他看不到类。有什么建议、建议、链接吗?谢谢

请尝试以/结束您的路径。作为URLClasseLoaderUrl[]的文档 说:

假定以/结尾的任何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-URLfile:\\d:\\runtime-EclipseApplication\\TestingProject\\bin\\org\\eclipse\\testing/;现在我得到:java.lang.NoClassDefFoundError:Test错误名称:org/eclipse/testing/TestEdited。试试我的例子。是的,很有效,谢谢。我不知道为什么它会从try/catch块中丢失所有变量值,即使我在try/catch块之前声明了它们。但感谢这一点:记住一条简单的规则:在所有平台上,URL总是使用前斜杠/。
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();
}