在jar运行时从桌面获取PNG

时间:2019-03-07 17:43:29

标签: jar io

我正在IDE上开发一个项目,当然,使用它时,我将文件保存到资源中并使用它们没有问题。虽然,我在raspbian系统上运行项目的.jar,并且图像文件保存在Desktop上,与jar所在的目录相同,实际上不是问题,主要问题是我需要访问该文件它不让我给我FileNotFoundException。这是代码:

ImageIO.write(bi, "png", new File("mandelbrot.png"));
SendPhoto spr = new SendPhoto();
spr.setChatId(update.getMessage().getChatId());
spr.setReplyToMessageId(update.getMessage().getMessageId());
spr.setPhoto(new File("home/pi/Desktop/mandelbrot.png"));    //I've also tried with ~/Desktop/mandelbrot.png

确切的错误如下:

Mar 07, 2019 6:24:58 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.io.FileNotFoundException) caught when processing request to {s}->https://api.telegram.org:443: home/pi/Desktop/mandelbrot.png (No such file or directory)
Mar 07, 2019 6:24:58 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://api.telegram.org:443
Mar 07, 2019 6:24:58 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.io.FileNotFoundException) caught when processing request to {s}->https://api.telegram.org:443: home/pi/Desktop/mandelbrot.png (No such file or directory)
Mar 07, 2019 6:24:58 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://api.telegram.org:443
Mar 07, 2019 6:24:58 PM org.apache.http.impl.execchain.RetryExec execute
INFO: I/O exception (java.io.FileNotFoundException) caught when processing request to {s}->https://api.telegram.org:443: home/pi/Desktop/mandelbrot.png (No such file or directory)
Mar 07, 2019 6:24:58 PM org.apache.http.impl.execchain.RetryExec execute
INFO: Retrying request to {s}->https://api.telegram.org:443

我对jar文件的工作原理一无所知,所以我认为我做错了,因为否则该目录就没有意义了。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,这是一个误会。我只需要写mandelbrot.png而不是全部写到路径就可以到达文件,因为它在同一目录中。我将其保留,以防有人需要。