bontade
bontade

Reputation: 3224

Add library as "Referenced library"

I'm trying programatically add library to referenced libraries. Here's my code:

String filename = "myfile.jar";
InputStream is;
try {
   is = new BufferedInputStream(new FileInputStream("D:\\" + filename));
   IFile file = project.getFile(filename);
   file.create(is, false, null);

   IPath path = file.getFullPath();                         
   IClasspathEntry[] cpe = javaProject.getRawClasspath();
   List<IClasspathEntry> libraries = Arrays.asList(cpe);                    
   libraries.add(JavaCore.newLibraryEntry(path, null, null));
   try {
        javaProject.setRawClasspath(libraries.toArray(new IClasspathEntry[libraries.size()]), null);
   } catch (JavaModelException e1) {
        e1.printStackTrace();
   }
 } catch (FileNotFoundException e) {
   e.printStackTrace();
 }

But the result seems that:

enter image description here


UPDATE 1.

Here's classpath. It seems that .classpath isn't changed.

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry kind="con"  path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

Upvotes: 0

Views: 582

Answers (3)

bontade
bontade

Reputation: 3224

This solution works:

IClasspathEntry[] entries = javaProject.getRawClasspath();
IClasspathEntry[] newEntries = new IClasspathEntry[entries.length + 1];

 System.arraycopy(entries, 0, newEntries, 0, entries.length);

 // add a new entry using the path to the container
 //Path junitPath = new Path("D:\\jarscan.jar");
 IClasspathEntry junitEntry = JavaCore.newLibraryEntry(new Path("D:\\jarscan.jar"), null, null, false);
 newEntries[entries.length] = junitEntry;
 javaProject.setRawClasspath(newEntries, null);

More info here

Upvotes: 0

Konstantin Komissarchik
Konstantin Komissarchik

Reputation: 29139

I suspect that the issue is with this line:

IPath path = file.getFullPath();

Try this line instead:

IPath path = file.getProjectRelativePath();

A good way to debug problems when making changes to project build path is to look at the .classpath file in the project root. This file will show you the exact effect your code is having. Then compare to the effect you get when performing an equivalent operation manually.

Upvotes: 1

aphex
aphex

Reputation: 3412

Here is a snippet how to add custom libraries to the project. I hope it helps you to get the idea.

// configure class path, source dir and output dir
        final IPath outputpath = project.getFullPath().append(CLASSES);
        final IClasspathEntry classpath1 = JavaCore.newSourceEntry(project.getFullPath().append(customPath));
        final IClasspathEntry[] defaultClasspath = org.eclipse.jdt.ui.PreferenceConstants.getDefaultJRELibrary();
        final IClasspathEntry[] newClasspath = new IClasspathEntry[defaultClasspath.length + 2];
        System.arraycopy(defaultClasspath, 0, newClasspath, 0, defaultClasspath.length);
        newClasspath[defaultClasspath.length] = classpath1;
        javaProject.setRawClasspath(newClasspath, outputpath, null);

Upvotes: 0

Related Questions