views:

444

answers:

1

Hello everyone!

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!

+1  A: 

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.

Brian Agnew
The error shows me that it cannot open the following url: jar:file:C:/Users/gosling/.../cancer_registry/editor/dist/editor.jar!/myeditor/resources/help/help.htm
gosling
So that URL is pointing to within the .jar file. Your browser won't understand that, and you should extract that file via getResourceAsStream() to a temp dir, and open it from there.
Brian Agnew
If the resource "/myeditor/resources/icddb" is a database, then you will have the same problem there. In addition, you have the problem of where updates your application makes to the database will be stored. You don't want to try to update the database in the JAR file!
Stephen C
Still trying to fix it... Thank you very much for your answers. You helped me locate the problem. So, I try to copy each content of the directory that contains for example the database's files by using getResourceAsStream(), and then read InputStream and write to OutputStream. The problem is that when I run the jar file, when I say e.g. icddb_directory.list() in order to access the folder's contents and copy them, it doesn't recognize icddb as a directory and so it doesn't return any contents to copy..
gosling