问题描述:

I'm developing an Eclipse plugin which, on startup, needs to go through all open Java projects in the workspace, get each ones resolved classpath and do something with it. The code I have so far looks something like this:

public void start(BundleContext context) throws Exception {

for (IProject project : ResourcesPlugin.getWorkspace().getRoot().getProjects()) {

if (project.isOpen() && project.getNature(JavaCore.NATURE_ID) != null) {

IJavaProject javaProject = JavaCore.create(project);

for (IClasspathEntry cpe : javaProject.getResolvedClasspath(true)) {

// Do something with cpe

}

}

}

}

The problem is that for some projects in the workspace IJavaProject.getResolvedClasspath(true) initially returns an empty classpath. My guess is that those projects aren't fully loaded yet.

I also have a IResourceChangeListener which listens for projects opening/closing and does the same thing as what is done on startup. When I close and reopen a project that initially had an empty classpath IJavaProject.getResolvedClasspath(true) will return the expected classpath.

Is my assumption correct that those projects haven't been fully loaded yet? How should I redesign this to get a proper resolved classpath for all Java projects on startup of my plugin?

网友答案:

Figured it out on my own from the Eclipse JDT source code. The IJavaProject.getResolvedClasspath(boolean) can indeed return an empty classpath if the classpath is being resolved by another thread. Adding a call to JavaCore.initializeAfterLoad(IProgressMonitor) before trying to get the resolved classpath for a project seems to do the trick.

相关阅读:
Top