[英]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.