在jar文件里面的java文件不是打开的 [英] File is not opening in java which is inside jar file

查看:204
本文介绍了在jar文件里面的java文件不是打开的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开我的Java应用程序的文件形式。使用以下代码:


代码:

if(Desktop.isDesktopSupported()){
try {
File myFile = new File(/ path / to / file.pdf);
Desktop.getDesktop()。open(myFile);
} catch(IOException ex){
//没有为PDF申请注册
}
}

当我使用如下路径:

 C:\\Users\\ \\\ kalathoki \\\ Documents \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ pre> 

打开。但是我的文件在我的包里面

  files / test.pdf 

我用了

  files \\test.pdf 

显示以下异常:

<$ p $线程AWT-EventQueue-0中的异常java.lang.IllegalArgumentException:文件:\files\test.pdf不存在。

为什么?任何想法...我想包括我的文件在我的jar文件,可以打开我的应用程序,每当用户想要。

谢谢...

解决方案

getDesktop#open 只允许从文件系统打开文件。一种解决方案是将PDF文件本地保存在文件系统上并从那里读取。这消除了从JAR本身提取文件,所以效率更高。


I tried to open file form my java application. Using following code from

Open PDF file on fly from Java application

Code:

if (Desktop.isDesktopSupported()) {
    try {
        File myFile = new File("/path/to/file.pdf");
        Desktop.getDesktop().open(myFile);
    } catch (IOException ex) {
    // no application registered for PDFs
    }
}

When I use path like :

"C:\\Users\\kalathoki\\Documents\\NetBeansProjects\\TestJava\\src\\files\\test.pdf" 

it opens. But my file is inside my package

files/test.pdf 

and I used

files\\test.pdf 

it shows following exception:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: The file: \files\test.pdf doesn't exist.

Why? Any Idea... I want to include my file inside my jar file that can open from my application whenever user wants.

Thanks...

解决方案

getDesktop#open only allows files to be opened from the file system. One solution is to keep the PDF file locally on the file system and read from there. This eliminates extracting the file from the JAR itself so is more efficient.

这篇关于在jar文件里面的java文件不是打开的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆