[英]Accessing PDF file stored in a .jar
我正在創建一個GUI,並且打算添加一個簡單的pdf文件作為幫助文件。 我將其包含在資源包中,並在此板上搜索以下代碼:
private void GuideActionPerformed(java.awt.event.ActionEvent evt) {
if (Desktop.isDesktopSupported()) {
try {
File myFile = new File(("Help.pdf"));
if (!myFile.exists()) {
// In JAR
InputStream inputStream = ClassLoader.getSystemClassLoader()
.getResourceAsStream("resources/manual.pdf");
// Copy file
OutputStream outputStream = new FileOutputStream(myFile);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
inputStream.close();
}
Desktop.getDesktop().open(myFile);
} catch (IOException ex) {
// no application registered for PDFs
}
}
問題在於,現在pdf閱讀器無法訪問該文件,因為Java應用程序已使用該文件。 有辦法解決嗎? 否則,我只會將文件添加到dist並確定資源路徑。
我嘗試了您的代碼,它運行良好,請檢查您的源代碼中是否使用文件manual.pdf
創建了文件夾resources
。 同樣,在創建jar
請檢查是否已導出這些文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.