org.eclipse.ui.jarpackager 套件可提供 public 程式類別,讓您以程式設計方式將檔案匯出到 JAR 檔中。 以下的程式碼片段是說明 JarPackageData 類別的用法:
void createJar(IType mainType, IFile[] filestoExport) {
Shell parentShell= ...;
JarPackageData description= new JarPackageData();
IPath location= new Path("C:/tmp/myjar.jar");
description.setJarLocation(location);
description.setSaveManifest(true);
description.setManifestMainClass(mainType);
description.setElements(filestoExport);
IJarExportRunnable runnable= description.createJarExportRunnable(parentShell);
try {
new ProgressMonitorDialog(parentShell).run(true,
true, runnable);
} catch (InvocationTargetException e) {
// An error has occurred while executing
the operation
} catch (InterruptedException e) {
// operation had been canceled.
}
}