5

I have a jar file, that holds various folders like images, fonts, messages etc... I have to read all the files inside the font folder only.

At present my java code is iterating through all the contents in the jar file. My code is as follows:

private void loadApplicationSpecificFonts() {
        try{
                JarFile jarFile = new JarFile("owenstyle-jar-config.jar");
          JarEntry entry;
                String fontName;

    for(Enumeration em = jarFile.entries(); em.hasMoreElements();) {
                    String s= em.nextElement().toString();
                    if(s.endsWith("ttf")){
                        fontName= s.substring(s.lastIndexOf("/")+1);
                        fontName= fontName.substring(0, fontName.indexOf(".ttf"));
                        entry = jarFile.getJarEntry(s);
                        InputStream input = jarFile.getInputStream(entry);
                        Font font= Font.createFont(Font.TRUETYPE_FONT, input);
                        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
                        ge.registerFont(font);

                        input.close();
               }
    }
           jarFile.close();
        }catch (IOException e){
          System.err.println("Error: " + e.getMessage());
        }catch (FontFormatException e){
          System.err.println("Error: " + e.getMessage());
        }
    }

Is there a way such that I can give the path of the font folder(congig-jar\config\assets\fonts), rather than traversing through all the contents in the jar. I know the path of the font folder is fixed, so I do not want the overhead of travesring through all the folders in the jar.

nishMaria
  • 135
  • 3
  • 11
  • Maybe this piece of code works? `JarEntry entry = getJarEntry("congig-jar\config\assets\fonts");` `InputStream input = jarFile.getInputStream(entry);` – Mike Lin Mar 02 '12 at 10:40
  • 1
    Who made (makes) the Jar? If it is you, it would make sense to put a list of the included files in a known location in the Jar. E.G. `fonts/all.list`. Get that single resource, read it, and you have the list of font entry names. – Andrew Thompson Mar 02 '12 at 11:29
  • Andrew- The jar file is not made by me. It is assmebled and send, and I have to read the font files inside my font folder in the jar – nishMaria Mar 02 '12 at 12:39
  • Basically i need a way to read from folders inside a jar file – nishMaria Mar 02 '12 at 13:15
  • if the jar is sent to you, you can still add a fonts/all.list metafile by preprocessing the file in a small program or script before you ship/deploy it. Preprocessing means unzip, iterate over files, create list file, repack jar with new file included. – rompetroll Mar 02 '12 at 14:19

2 Answers2

3

You can utilize a Classloader to put the jar on you classpath. and then all files inside the jar are loadable resources.

//ref to some jar
File f = new File("/tmp/foo.jar");

//create a new classloader for this jar
URLClassLoader loader = URLClassLoader.newInstance(new URL[]{f.toURI().toURL()});

//load resource with classloader
InputStream inputStream = loader.getResourceAsStream("foo/bar/test.txt");

//...do stuff with inputStream
rompetroll
  • 4,781
  • 2
  • 37
  • 50
  • 1
    I have several files inside my font folder. So i need a way such that 1) I give the direct path of the folder inside my jar file(This way I will not have to first init the jar and then read the contents inside one by one and see if the file extension is .ttd) 2) Iterate through the folder one by one dynamically and register them in the graphics enviornment – nishMaria Mar 02 '12 at 13:09
  • I'm afraid there is no way to to this using a ClassLoader. A good advice is what Andrew Thompson suggests in his comment to your question: store a file inside the jar that lists all font pathes; then read the file and iterate over the stored pathes. – rompetroll Mar 02 '12 at 14:12
1

you can use

Class.getResourceAsStream ("/pkg/resource_name");

this code will start to search for resources from the classpath.

http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html

ejrav
  • 56
  • 1
  • 3
  • If I have to use Class.getResourceAsStream ("/pkg/resource_name"), then I need to give the exact name of the font file. My issue is, I have several font files inside my font folder. So I need a way to first give the path of the folder inside the jar file and then dynamically iterate through all the files inside the font folder and then register them. – nishMaria Mar 02 '12 at 13:32
  • I found this post [http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file](http://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file). – ejrav Mar 02 '12 at 14:31
  • My requirment is basically bot to iterate through all the files/folders in the jar, but just to iterate through the font folder since i know the pacth where the font files reside. So is there a way such that, I give the direct path of the font folder in the jar and then iterate through the font folder and read the font files one by one? – nishMaria Mar 05 '12 at 15:26