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) { // 執行作業時發生錯誤 } catch (InterruptedException e) { // 已取消作業。 } }
另外會提供 API,讓您建立 JarPackageData 的外掛程式特定子類別。 這可讓其他外掛程式實作本身的 JAR 匯入/匯出精靈, 並將 JarPackageData 物件的內容儲存到對應的 JAR 說明檔中。
在 JarPackageData 說明了 JAR 之後, 即可使用 JarWriter2 以程式設計方式加以撰寫。