2017-05-08 110 views
0

如何将IPath转换为URL。就我而言,我想将Java项目的输出位置转换为URL,以便我可以从那里加载URLClassLoader的类。我的代码如下所示:Eclipse - 将IPath转换为URL

javaProject.getProject().build(IncrementalProjectBuilder.FULL_BUILD, null); 

URL url = new File(javaProject.getOutputLocation().toFile().getAbsolutePath()).toURI().toURL(); 
URL[] urls = new URL[] { url }; 
URLClassLoader classLoader = new URLClassLoader(urls); 
Class<?> c = classLoader.loadClass(fully-qualified-class-name); 

的问题是,该URL只是一个相对的一个,也就是它的字符串表示是file:/C:/com.florianingerl.regexfindandreplace.matchevaluators/bin,但它应该是这样的file:/C:/Users/Hermann/runtime-EclipseApplication/com.florianingerl.regexfindandreplace.matchevaluators/bin

URLClassLoader找不到类明显。

任何帮助表示赞赏。

回答

0

您可以使用类似:

IPath path = javaProject.getOutputLocation(); 

IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path); 

URI uri = file.getLocationURI(); 

URL url = uri.toURL();