简体   繁体   中英

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. 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. 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 . 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. 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.

JarWriter3 creates a new jar file but does not provide any methods to add to a jar that already exists. I have searched both the Eclipse documentation and the stackoverflow archive, so far without success. 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.

JarWriter3 is essentially just a fairly thin wrapper around JarOutputStream , for example the constructor opens the jar output stream with:

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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