Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically adding a library to an Eclipse project

How can I create a new build path entry for any *.jar file and add this classpath entry to the build path of an Eclipse project.

I have a plugin that should automatically setup my target project. So this project needs to have some library imports and I want to add this imports automatically using a wizard. The user just selects the location of a certain SDK and then some libraries have to be linked with the target project.

However, I found some references:

Importing libraries in Eclipse programmatically

How to add a folder to java build path as library, having multiple jars or entries in it?

Unfortunately, I failed to implement the second solution as I cannot find the classes IClasspathContainer, JavaCore and IJavaProject.

I'm using Eclipse Helios and JDK. Do I need any additional libraries to make changes to the build path or is there a simpler solution to import a jar library programmatically?

Regards, Florian

like image 531
Florian Avatar asked Dec 01 '10 15:12

Florian


2 Answers

I'm assuming that you are creating a plugin and need your plugin to manage the extra jars added to the classpath.

As you mention, you need to create a custom classpath container. First, create the classpath container extension by exending this extension point:

org.eclipse.jdt.core.classpathContainerInitializer

Then, you create a class that implements org.eclipse.jdt.core.IClasspathContainer and associate it with the extension point you just created.

You mention that you cannot find the org.eclipse.jdt.core.IClasspathContainer interface. You need to make sure that your plugin references the org.eclipse.jdt.core plugin in its MANIFEST.MF.

like image 97
Andrew Eisenberg Avatar answered Sep 30 '22 05:09

Andrew Eisenberg


Here you can find some examples, how to define new classpath entries and classpath containers to java projects. I think it would handy for someone reading this question.

like image 20
aphex Avatar answered Sep 30 '22 05:09

aphex