![](/img/trans.png)
[英]Apache POI on an exported .jar is not running on a different computer
[英]Apache poi java: Image not showing in document when running JAR file in another computer
我創建了一個可以處理文件的應用程序,並且可以在我編寫代碼的計算機上完美運行,但是它在其他計算機上無法正常運行。 在查看了整個代碼后,我能夠找到問題所在。 我刪除了以下添加圖像的方法,jar 文件在多台計算機上工作正常,但是我需要添加圖像。 方法如下:(文檔是在另一個 class 中聲明的 static 變量,以防您想知道)
public void AddImage() throws IOException, InvalidFormatException {
XWPFParagraph parag = document.createParagraph();
XWPFRun r = parag.createRun();
URL imageURL = ClassLoader.getSystemResource("TheImage.png");
String imageName = imageURL.getPath();
File image = new File(imageName);
FileInputStream fis = new FileInputStream(image);
BufferedImage bimg1 = ImageIO.read(image);
int width = 160;//bimg1.getWidth();
int height = 26;//bimg1.getHeight();
String imgFile = image.getName();
r.addPicture(fis, document.PICTURE_TYPE_PNG, imgFile, Units.toEMU(width), Units.toEMU(height));
}
為了給你更多的細節,我在 eclipse 的項目中創建了一個源文件,我在其中添加了圖像,有人知道如何解決這個問題嗎?
也許在您的計算機上,圖像是從文件系統中獲取的,而不是從 jar 獲取的。 鏡像是否打包在 jar 文件中? 然后嘗試
Inputstream logo = getClass().getResourceAsStream("/path/in/jar/img.png");
加載它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.