我正在尝试以编程方式将库添加到引用的库中。这是我的代码:
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();
}
但结果似乎是:
更新 1。
这是类路径。似乎.classpath
没有改变。
<?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>