Results 1 to 15 of 15
  1. #1
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Post java.io.File from Jar File

    Hey ppl i am struggling to get resources from a jar file.
    when the my app is not complied to a jar file the following code works, however the moment i create the jar file it does not find the file, i have searched Google endlessly, but could not find the solution.

    Java Code:
    File chmfile=new File(this.getClass().getResource("text3D.chm").toURI());
    I also tried the following, but same happens

    Java Code:
    File chmfile=new File(this.getClass().getResource("text3D.chm").getFile());
    File chmfile=new File(this.getClass().getResource("text3D.chm").getPath());
    in the end this is what i hope to achieve: 01 run the help file from the jar, 02 copy some resource files from the jar to the local hd

    Please any help will be appreciated:)

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Do you get an exception when you run your program?

  3. #3
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Packaged resources can't be accessed as files, because they aren't files. Since you're trying to access it as a File, I presume you are wanting to read it in?

    If so, use 'getResourceAsStream', and you can read it as you want. If you need it to be a File for some reason, you can read it in as a stream and write it back out as a temporary file. Alternately, if you want to modify it, you can use the java.util.jar package to directly read and write (not recommended if you're running from a jar to write to that same jar while running....) entries in a jar.

  4. #4
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Post

    Here is the Exception for one of the files i would like to copy to the local jre bin folder

    java.io.FileNotFoundException: file:\C:\JDeveloper\mywork\text3D\text3D\deploy\t
    ext3D_beta_1.jar!\text3d\jogl.dll (The filename, directory name, or volume label
    syntax is incorrect)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(Unknown Source)
    at text3d.InstallJOGL.copyfile(InstallJOGL.java:29)
    at text3d.InstallJOGL.run(InstallJOGL.java:16)
    at text3d.main.<init>(main.java:12)
    at text3d.main.main(main.java:36)

  5. #5
    oomrichie is offline Member
    Join Date
    Feb 2011
    Posts
    31
    Rep Power
    0

    Thumbs up

    Thanks toadaly..
    i used your advice and found a solution

    Java Code:
        private void installjavadll(String filename) throws Exception{
            InputStream in = this.getClass().getResourceAsStream(filename);
            String path = System.getProperty("java.library.path").substring(0, System.getProperty("java.library.path").indexOf(";"))+ "\\" + filename;
            File filejogl = new File(path);        
            OutputStream out = new FileOutputStream(filejogl,true);        
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            in.close();
            out.close(); 
            System.out.println("Copy Successful - " + path);
        }

  6. #6
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Your problem with that earlier path was that you had to change "file:" to "jar:"

  7. #7
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    @toadaly
    It is possible to read files in from a jar file (or any zip file in that matter).
    Java Code:
    jar:\C:\path\to\file.jar!\file\inside\jar.txt
    That's what Class.getResource() returns in URL if the file is in the local jar.

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    ra4king, yes, you can use the URL returned from getResource. What you can't do, is what the OP was trying to do, which is to create a java.io.File from that and then use the File oriented IO routines. You can only create java.io.File's from URI's that are "file:" URIs. Thats'' what the API states, and if you try to violate the API is doesn't work.

  9. #9
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Ah ok. How about:
    Java Code:
    File file = new File(new URL("jar:\C:\path\to\file.jar!\file\inside\jar.txt").toURI());
    ;)

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    From the API:

    Java Code:
    File(URI uri) 
              Creates a new File instance by converting the given [B]file:[/B] URI into an abstract pathname
    It must be a "file:" URI, not a "jar:" URI.

    You can successfully create a File object from any String, but that object will not be of use in the File io routines if it is not a type the OS considers a file. Please try it and verify for yourself that it doesn't work, since you don't seem to believe it.

  11. #11
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Which is why i did "new URL("...").toURI" ;)

  12. #12
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    ra4king, *it will not work*. Please try it.

    Create a file junk.java with the following contents:

    Java Code:
    import java.io.*;
    import java.net.*;
    
    public class junk {
    
        public static void main(String[] args) throws Exception {
    
    	URL url = new junk().getClass().getResource("junk.class");
    
    	System.out.println(url);
    
    	File file = new File(url.toURI());
    
    	System.out.println(file.exists());
    
        }
    
    }
    javac junk.java
    jar cf junk.jar junk.class
    java -cp junk.jar junk

    ...here's what happens:

    Java Code:
    jar:file:/C:/cygwin/home/dad/junk.jar!/junk.class
    
    Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
    
    	at java.io.File.<init>(Unknown Source)
    
    	at junk.main(junk.java:12)
    The reason for the error, is that the URI is not a "file:" URI. Now try running it as:

    java junk

    The result is then as expected, because "junk.class" is now a "file:" uri. Here are my results (which depend on your file structure):

    Java Code:
    file:/C:/cygwin/home/dad/junk.class
    
    true

  13. #13
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Huh, you're right I just did some testing and "toURI()" doesn't work with a jar file.
    Quick question: since I've never used URI before, what does it mean? How does it differ from URL?

    EDIT: Actually, with further experimenting, that exception happens because File can't handle the "!/junk.class" part, aka it is not part of the normal hierarchical file system. Not URI's fault :/
    Last edited by ra4king; 04-22-2011 at 05:38 AM.

  14. #14
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    File can't handle "jar:", because "jar:" != "file:" and File only works with "file:" URI's. It's right there in the API.

    I'm being evil about this, because this is my 666th post.

  15. #15
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    666th post! Hahaha. Well thanks you devil you!

    EDIT: So if we need any file from a jar, we just avoid File and stick with URL :)

Similar Threads

  1. how to split large xml file into small xml file in java
    By enggvijaysingh@gmail.com in forum XML
    Replies: 2
    Last Post: 02-07-2011, 10:34 AM
  2. Replies: 0
    Last Post: 02-11-2009, 10:53 AM
  3. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 10:57 AM
  4. Replies: 7
    Last Post: 05-23-2008, 04:46 AM
  5. How to read a text file from a Java Archive File
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •