[英]eclipse plugin > how to include a jar file programmaticaly
所以...,我已經為Eclipse創建了一個插件,它可以從模板等生成一個新的java項目和廣告文件......然而,/ src目錄中的代碼無法編譯,因為我需要添加一個jar文件到庫標簽。
該項目已經是一個Java項目,通過:
org.eclipse.jdt.core.IJavaProject javaProject = org.eclipse.jdt.core.JavaCore.create(proj);
org.eclipse.jdt.core.IClasspathEntry src = JavaCore.newSourceEntry(folder.getFullPath());
IClasspathEntry jre = JavaCore.newContainerEntry(new Path(
org.eclipse.jdt.launching.JavaRuntime.JRE_CONTAINER), new IAccessRule[0],
new IClasspathAttribute[] {
JavaCore.newClasspathAttribute("owner.project.facets", "java")
}, false);
IClasspathEntry[] entries = new IClasspathEntry[] {
src, jre
};
javaProject.setRawClasspath(entries, proj.getFullPath().append("bin"), new NullProgressMonitor());
而現在,基本上,我需要按程序設計,“Add Jars ...”按鈕的功能。
一段時間以來一直在努力...
任何代碼提示或指向教程的鏈接都會有所幫助。 請不要鏈接到通用Eclipse插件教程:)因為我現在可能已經看到它們了...
Thnx很多
不知道如何通過eclipse API執行此操作,但是所有jar配置窗口都會寫入您的project-name/.classpath
文件,其類似於:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="lib" path="x-jars/lucene-fast-vector-highlighter-3.0.3-patch1822.jar"/>
<classpathentry kind="lib" path="x-jars/lucene-highlighter-3.0.3.jar"/>
因此,一個選項是讓您的設置代碼在項目創建后編輯此文件,但這可能是您想要的太多黑客。
這是我如何做到的,不確定你的req是否相同,但希望它在某種程度上有所幫助......
IFile file = addJar(project, "/resources/myJar.jar", MY_JAR_TARGET_PATH, monitor); //$NON-NLS-1$
newcpEntries.add(JavaCore.newLibraryEntry(file.getFullPath(), null, null, false));
// .....
其中addJar()看起來像這樣:
private static IFile addJar(IProject project, String srcPath, String targetPath, IProgressMonitor monitor) {
URL srcURL = MyPlugin.getDefault().getBundle().getEntry(srcPath);
IFile file = project.getFile(targetPath);
InputStream is = null;
try {
is = srcURL.openStream();
file.create(is, true, monitor);
} catch (CoreException e) {//...
} catch (IOException e) {//...
}
finally {
try {
if (is != null)
is.close();
} catch (IOException ignored) {}
}
return file;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.