Programmatically adding a library to an Eclipse project
Asked Answered
P

4

7

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

Promulgate answered 1/12, 2010 at 15:0 Comment(0)
H
1

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.

Hydrography answered 4/12, 2010 at 23:41 Comment(1)
I forgot to include the Java Developer Tools after I downloaded them. Having that fixed, I used the tutorial aboth to create a custom class container.Promulgate
P
1

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.

Protochordate answered 26/3, 2013 at 13:39 Comment(0)
S
0

In order to get access to IJavaProject etc, goto your plugin.xml and add org.eclipse.jdt.core to the classpath. Thereafter you can import those packages into your project.

Sukkoth answered 25/8, 2014 at 9:56 Comment(0)
C
0
String projectName = "MyProject"; // project to add a library to
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
IJavaProject jProject = JavaCore.create(project);

for(File file : new File("path-to-some-directory-of-libraries-to-add").listFiles()){
    if(file.isFile() && file.getName().endsWith(".jar")){
        addProjectLibrary(jProject, file);
    }
}

private static void addProjectLibrary(IJavaProject jProject, File jarLibrary) throws IOException, URISyntaxException, MalformedURLException, CoreException {
    // copy the jar file into the project
    InputStream jarLibraryInputStream = new BufferedInputStream(new FileInputStream(jarLibrary));
    IFile libFile = jProject.getProject().getFile(jarLibrary.getName());
    libFile.create(jarLibraryInputStream, false, null);

    // create a classpath entry for the library
    IClasspathEntry relativeLibraryEntry = new org.eclipse.jdt.internal.core.ClasspathEntry(
        IPackageFragmentRoot.K_BINARY,
        IClasspathEntry.CPE_LIBRARY, libFile.getLocation(),
        ClasspathEntry.INCLUDE_ALL, // inclusion patterns
        ClasspathEntry.EXCLUDE_NONE, // exclusion patterns
        null, null, null, // specific output folder
        false, // exported
        ClasspathEntry.NO_ACCESS_RULES, false, // no access rules to combine
        ClasspathEntry.NO_EXTRA_ATTRIBUTES);

    // add the new classpath entry to the project's existing entries
    IClasspathEntry[] oldEntries = jProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = relativeLibraryEntry;
    jProject.setRawClasspath(newEntries, null);
}

Note that as Andrew Eisenberg mentioned, you need to include the org.eclipse.jdt.core plugin dependency in your plugin's MANIFEST.MF.

Note that you may also need to programmatically refresh the project too.

Concern answered 14/4, 2017 at 21:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.