Importing libraries in Eclipse programmatically

冷暖自知 提交于 2019-12-05 22:15:58

This did the trick. What I wanted exactly is importing the library into the project and then referencing it from the project not using a reference to an external file.

    InputStream is = new BufferedInputStream(new FileInputStream("C:\\myfolder\\mylibrary.jar"));
    IFile file = project.getFile("mylibrary.jar");
    file.create(is, false, null);

    IPath path = file.getFullPath();
    libraries.add(JavaCore.newLibraryEntry(path, null, null));
    //add libs to project class path
    try {
       javaProject.setRawClasspath(libraries.toArray(new IClasspathEntry[libraries.size()]), null);
    } catch (JavaModelException e1) {
       e1.printStackTrace();
    }

IFile.getRawLocationURI() gets you an absolute path

setRawClasspath() is the right method.

However, you need first to copy your jar to the root directory of your project before adding it (with the new path) to the classpath of the project.
That way, the relative path of the jar will be jtwitter.jar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!