简体   繁体   中英

java- jar file cannot find resources

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).

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"));

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", "");

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!

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM