Re: Accessing files within JAR archive

From: Alex Kizub (akizub_at_yahoo.com)
Date: 10/30/03


Date: 30 Oct 2003 09:51:23 -0800

String file="test.txt";
URL url = getClass().getResource("/file/txt/"+file);
if (url == null) {
        System.out.println(
                "File "+file+" do not exist.");
        break;
}
URLConnection conn = url.openConnection();
InputStream istream = conn.getInputStream();

Important thing is "/file/txt/"!
This is path inside jar.
If you use just file it means current directory and it means where is
you class located.
So, if you call this method in class a.b.Test then it will
read /a/b/file.txt inside jar.

Alex Kizub.



Relevant Pages

  • Yet another way to use Java
    ... * @param mainClass e.g. FontShower, ... StompProject (String mainClass, ... * does this project have a jar ... void mkDescBtm() ...
    (comp.lang.java.programmer)
  • Re: Find my "program" folder?
    ... it doesn't work with an empty string. ... jar package returns Null. ... from the IDE: ... I want to convert this into a File object. ...
    (comp.lang.java.help)
  • Re: dll in jar?
    ... Both of these C functions take a string which gets mapped to a file. ... Niether of these C functions know how to load libraries out of jar or zip files. ... a look at how webstart does it, or just use webstart for deployment. ... beyond the scope and control of the JVM and the JVM has to fit within ...
    (comp.lang.java.programmer)
  • text file cant be found inside a jar
    ... Click the link below to run the applet: ... Click the link below to download the jar file: ... String message=new String; ... BufferedReader buffer = null; ...
    (comp.lang.java.help)
  • Re: Help : ImageIcon constructor fails to load image
    ... > a strange problem. ... > protected static ImageIcon createImageIcon(final String path, ... filesystem is slightly different than the technique for getting a jar entry ... entry out of a jar in the classpath. ...
    (comp.lang.java.help)