java-jar文件找不到资源 [英] java- jar file cannot find resources

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

问题描述

我正在开发一个 Java 应用程序,在该程序中我使用了一些文件,例如图像、本地数据库和 .htm 文件(用作帮助文件).

I 'm working on a java application and in the program I use some files such as images, a local database and an .htm file (used as help file).

我已尝试使访问这些资源的权限与位置无关.所以我在包含我的源文件的包中制作了一个名为 resources 的子文件夹,并将所有这些图像等放在那里.我通过 .getResource() 方法访问它们,而不是使用路径.例如,这就是我访问图像资源的方式:

I have tried to make the access to these resources, location independent. So I've made in the package that contains my source files, a subfolder named resources and I've put there all these images,etc. I access them through .getResource() method, and not by using paths. For example that's how i access an image resource:

lang_icon=new javax.swing.ImageIcon(getClass().getResource("/myeditor/resources/checked.gif"));

问题是当.jar 文件被构建时,它不能正常工作.图片加载成功,但无法连接本地数据库或打开.htm文件.
这是我用来访问 .htm 文件的代码(当我通过 Netbeans)

The problem is that when the .jar file is built, it doesn't work properly. It loads the images successfully but cannot connect to the local database or open the .htm file.
Here is the code I use to access the .htm file (it works fine when I run the application through Netbeans)

URL helpFile= getClass().getResource("/myeditor/resources/help/help.htm");

    try {
        BrowserLauncher launcher = new BrowserLauncher();
        launcher.openURLinBrowser(helpFile.toString());
    } catch (BrowserLaunchingInitializingException ex) {
        Logger.getLogger(mainAppFrame.class.getName()).log(Level.SEVERE, null, ex);
    } catch (UnsupportedOperatingSystemException ex) {
        Logger.getLogger(mainAppFrame.class.getName()).log(Level.SEVERE, null, ex);
    }

这是我用来访问本地数据库的代码

and here is the code I use to access my local database

URL url = getClass().getResource("/myeditor/resources/icddb");
    database = new File(url.getFile());
    try
       {
            Class.forName("org.hsqldb.jdbcDriver");

            con = DriverManager.getConnection("jdbc:hsqldb:file:" + database+"\\icddb", "sa", "");

当我尝试通过 .jar 文件打开 .htm 文件时,它显示此错误此文件没有与之关联的程序来执行此操作.在控制面板的文件夹选项中创建关联".
当我尝试连接到本地数据库时,它显示严重无法重新打开数据库".
所有想法表示赞赏!谢谢你,对不起我的英语!

When I try to open the .htm file through .jar file it shows this error "This file does not have a program associated with it for performing this action. Create an association in the Folder Options in control panel".
When I try to connect to my local database it says "Severe could not reopen database".
All ideas appreciated! Thank you and sorry for my english!

推荐答案

你能打印出你要求浏览器打开的 URL 吗?这应该能更清楚地表明正在发生的事情.

Can you print out the URL you're asking the browser to open ? That should give a clearer indication as to what's going on.

我怀疑您从 getResource() 获取的 URL 是指向 in .jar 文件的 URL.因此,浏览器将难以打开它.我会(也许)将该文件解压缩到一个临时目录并让浏览器打开它.

I suspect the URL you're getting from getResource() is a URL pointing to within your .jar file. As such the browser is going to have difficulty opening that. I would (perhaps) extract that file to a temporary directory and have the browser open that.

这篇关于java-jar文件找不到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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