开发者

How do I get the classpath, as file paths, of an IJavaProject in an Eclipse plugin?

I'm working on an Eclipse plugin that needs to build a classloader that can access all the things in the classpath of a project in Eclipse. I have an IJavaProject, and I'm trying to build a list of URLs to pass to a URLClassLoader:

final IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);
for (IClasspathEntry classpathEntry : resolvedClasspath) {
    urls.add(classpathEntry.getPath().makeAbsolute().toFile().getCanonicalFile().toURL());
}

But any project dependency (either manually added, or in the "Maven Dependencies" container from the Maven plugin) show up as a just "file:/projectName", instead of an absolu开发者_运维技巧te path.

Other experiments have involved using javaProject.getAllPackageFragmentRoots(), but this don't seem to include project dependencies inside the "Maven Dependencies" container.


The makeAbsolute() call isn't context aware. It just adds a leading "/".

You need to check if path is absolute (IPath.isAbsolute()). If not absolute, call IProject.getLocation() to get project root and concat the two paths.

TIP: The UrlClassLoader will lock all jars for the duration of that class loader's existence. This can lead to problems for users of your plugin as they will not be able to delete or change jars while your plugin is active. I've seen several workarounds for this. The most effective approach is to first copy jars to a temporary location and construct class loader with copies. Then you can monitor the original jars and update your copies and class loader without risking locking up user-controlled files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜