4

I need to programmatically check if a project's build path already contains a specified library. It's for a quick fix proposal, to know if this was already "fixed" and cannot be the problem.

I have access to the current IInvocationContext, and therefore (around some corners) to the corresponding IProject object.

How can I check which libraries are referenced in its build path already?

Cedric Reichenbach
  • 8,970
  • 6
  • 54
  • 89

1 Answers1

5
  1. Use JavaCore (in jdt.core) to create an IJavaProject from the IProject (use the create() method). This IJavaProject is an IProject with the ability to answer java environment questions about the project.
  2. The IJavaProject has several variants of the findType() method that you can use to query the classpath for the project

I'll leave it at that, but let me know if you need more info.

Chris Gerken
  • 16,221
  • 6
  • 44
  • 59
  • Thanks, that helped a lot! For others with the same problem: `IJavaProject.getRawClasspath()` gave me all entries and then I checked `entry.getEntryKind()` (in my case: `IClasspathEntry.CPE_CONTAINER`) and `entry.getPath()` if they fit my library. – Cedric Reichenbach Sep 21 '12 at 09:26
  • 1
    Thanks, that worked. Using `myProject.hasNature(JavaCore.NATURE_ID)`, you can check if the `create()` call will be successful. – barfuin Dec 12 '12 at 17:19