简体   繁体   中英

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 absolute 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM