简体   繁体   English

在eclipse插件中,以编程方式将新的.java文件保存到工作区中的现有jar文件中

[英]in eclipse plug-in, programmatically save new .java files to an existing jar file in the workspace

I'm writing a plug-in for Eclipse Standard/SDK Kepler to give diagnostic feedback to undergraduate student programmers as they work on their Java programming assignments. 我正在为Eclipse Standard/SDK Kepler编写一个插件,以向本科生程序员在处理Java编程作业时提供诊断反馈。 The student can run my plug-in as often as he wishes to see if his code triggers any messages about poor programming practices he might want to change. 该学生可以按自己希望的次数运行我的插件,以查看他的代码是否触发了有关他可能要更改的不良编程实践的任何消息。

Each time the student invokes the plug-in, I need to save a copy of all the .java files in the student's project as well as a text file containing the diagnostic feedback produced by the plug-in. 每次学生调用该插件时,我都需要在该学生的项目中保存所有.java文件的副本以及一个包含该插件产生的诊断反馈的文本文件。 I don't want to make the student use a file export wizard, so I figured out how to programmatically create a jar file using the JarPackageData and JarWriter3 classes from org.eclipse.jdt.ui.jarpackager . 我不想让学生使用文件导出向导,因此我想出了如何使用org.eclipse.jdt.ui.jarpackagerJarPackageDataJarWriter3类以编程方式创建jar文件的方法。 That works for the first time I save files, but I don't want to create a new jar each time the student runs the plug-in. 这是我第一次保存文件,但我不想每次学生运行插件时都创建一个新的jar。 When the student runs my plug-in for the second (third, fourth…) time I want to add more files to the jar I already made so that I end up with a single jar file that contains a complete record of the student's use of my plug-in. 当学生第二次(第三,第四…)运行我的插件时,我想向已经制作的jar中添加更多文件,以便最终得到一个包含该学生使用情况的完整记录的jar文件。我的插件。

JarWriter3 creates a new jar file but does not provide any methods to add to a jar that already exists. JarWriter3创建一个新的jar文件,但不提供任何添加到已经存在的jar中的方法。 I have searched both the Eclipse documentation and the stackoverflow archive, so far without success. 到目前为止,我都搜索了Eclipse文档和stackoverflow归档文件,但没有成功。 One answer to a previous question explained how to write a jar file using the java.util.jar.JarEntry and JarOutputStream classes, but I thought I should stick with functionality provided by Eclipse classes if indeed there are any that will solve my problem. 上一个问题的一个答案解释了如何使用java.util.jar.JarEntryJarOutputStream类编写jar文件,但是我认为如果确实有可以解决我的问题的方法,我应该坚持使用Eclipse类提供的功能。

JarWriter3 is essentially just a fairly thin wrapper around JarOutputStream , for example the constructor opens the jar output stream with: JarWriter3本质上只是JarOutputStream一个相当薄的包装器,例如,构造函数使用以下命令打开jar输出流:

if (fJarPackage.usesManifest() && fJarPackage.areGeneratedFilesExported()) {
  Manifest manifest = fJarPackage.getManifestProvider().create(fJarPackage);
  fJarOutputStream = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(fJarPackage.getAbsoluteJarLocation().toFile())), manifest);
} else
  fJarOutputStream = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(fJarPackage.getAbsoluteJarLocation().toFile())));

so I would be inclined to just use the standard Java JarOutputStream and JarEntry . 所以我倾向于只使用标准的Java JarOutputStreamJarEntry

If you are saving the jar in the workspace you will need to call IFile.refreshLocal once the jar is written to get it recognised by Eclipse. 如果要将jar保存在工作区中,则在编写jar以便使其能够被Eclipse识别后,将需要调用IFile.refreshLocal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM