This discussion is archived
5 Replies Latest reply: Jul 9, 2010 3:11 AM by 843804 RSS

Read filenames from jar-file

843804 Newbie
Currently Being Moderated
Hi,
I want to read image names in a jar-file directory in a web start application. This used to work in previous versions of jdk,
but when I use jdk 1.6.0_20 I get this errormessage:
"http:\server:8080\test\client.jar The filename, directory name, or volume label syntax is incorrect".
Why doesn´t this work anymore?

Some of my code looks like this:
     getFiles("images/toolbuttons/val/", ".gif", true);
     
     public String[] getFiles(String catalog, String fileNameEnd,
         boolean removePath)
     {
          JarFileFinder jarFileFinder = new JarFileFinder();
          String jarFilePath = jarFileFinder.getClassLocation(
              this.getClass().getName());
          String[] files = null;
          try
          {
               if (fileNameEnd != null)
               {
                    fileNameEndFilter = new FileNameEndFilter(fileNameEnd);
               }
               files = getFilesInJar(jarFilePath, fileNameEndFilter, catalog,
                   removePath);
          }
          catch (Exception e)
          {
               MessageUtil.messageOk(null, this.getClass().getName(), e.getMessage());
          }
          return files;
     }


     private String[] getFilesInJar(String jarFilePath, FilenameFilter filter,
         String catalog, boolean removePath)
         throws Exception
     {
          ZipFile zipFile = new ZipFile(jarFilePath);
          Enumeration e = zipFile.entries();
          String name;
          ZipEntry zipEntry;
          ArrayList filesList = new ArrayList();
          while (e.hasMoreElements())
          {
               zipEntry = (ZipEntry) e.nextElement();
               name = zipEntry.getName();
               if ((filter == null || !filter.accept(null, name)) ||
                   (catalog != null && !name.startsWith(catalog)))
               {
                    continue;
               }
               if (removePath == true)
               {
                    name = name.substring(name.lastIndexOf("/") + 1);
               }
               filesList.add(name);
          }
          String[] files = new String[filesList.size()];
          for (int i = 0; i < filesList.size(); i++)
          {
               String s = (String) filesList.get(i);
               files[i] = s;
          }
          return files;
     }





public class JarFileFinder
{
     public String getClassLocation(String classname)
     {
          try
          {
               Class clazz = Class.forName(classname);
               if (clazz == null) return null;

               URL url =
                   clazz.getProtectionDomain().getCodeSource().getLocation();
               String location = url.toString();

               if (location.startsWith("jar"))
               {
                    url = ((JarURLConnection)
                        url.openConnection()).getJarFileURL();
                    location = url.toString();
               }
               if (location.startsWith("file"))
               {
                    return new File(URLDecoder.decode(url.getFile(), "UTF-8")).getAbsolutePath(); 
               }
               else
               {
                    return URLDecoder.decode(url.toString(), "UTF-8"); 
               }
          }
          catch (Exception e)
          {
               MessageUtil.messageOk(null, this.getClass().getName(), e.getMessage());
          }
          return null;
     }
}
  • 1. Re: Read filenames from jar-file
    EJP Guru
    Currently Being Moderated
    The mistake here is that URLDecoder isn't for URLs, it is for arguments after ? in URLs, and POST form arguments. Just construct a URL object from the String url and then use its API to get out the name and path.
  • 2. Re: Read filenames from jar-file
    793415 Pro
    Currently Being Moderated
    Lisa_R wrote:
    ..Why doesn´t this work anymore?
    Sun considers it no business of client code to know the location of the web start cache. In any case, the web start cache can be moved by the administrator of the client machine at their whim. It is probably related to that.

    What functionality is the application aiming to provide by determining such information?
    Why is it it that a list of files cannot be included in the app. (for e.g. in a Properties file) at build time?
  • 3. Re: Read filenames from jar-file
    843804 Newbie
    Currently Being Moderated
    Thanks for your replies!!

    I tried to remove the URLDecoder, but the result was the same. I still get:
    "http:\server:8080\test\client.jar The filename, directory name, or volume label syntax is incorrect".
    AndrewThompson64 wrote:
    Lisa_R wrote:
    ..Why doesn´t this work anymore?
    Sun considers it no business of client code to know the location of the web start cache. In any case, the web start cache can be moved by the administrator of the client machine at their whim. It is probably related to that.

    What functionality is the application aiming to provide by determining such information?
    Why is it it that a list of files cannot be included in the app. (for e.g. in a Properties file) at build time?
    The reason why I want to read the filenames from the jar instead of including a list of the names is that if you
    want to add an image then all you have to do is include it in the directory and the program doesn´t have to be
    changed.
  • 4. Re: Read filenames from jar-file
    793415 Pro
    Currently Being Moderated
    Lisa_R wrote:
    ..I tried to remove the URLDecoder, but the result was the same. I still get:
    "http:\server:8080\test\client.jar The filename, directory name, or volume label syntax is incorrect".
    Given an URL should use '/' as opposed to '\', and starts with 2 '/' rather than one, it is not surprising that URL was rejected. Strange as it may seem, programming by typing random characters into an editor will rarely, if ever, work to create a working application.
    ..The reason why I want to read the filenames from the jar instead of including a list of the names is that if you
    want to add an image then all you have to do is include it in the directory..
    That is largely irrelevant, since the Jar needs to be freshly built, uploaded by the developer, and downloaded by the client. Since all of that needs to be done for the addition of a single image, you might as well add an Ant task to the build that provides a single file (like I suggested earlier) that lists the resources of interest. It would take less time to write an Ant task, than debug all the hoops the code is currently jumping through(1) in order to get the information of interest.

    1) And even if you got that working, it would be likely that Sun would introduce some change in the next version of Java that breaks it again. My advice is to stop fighting it, and go with the flow.
  • 5. Re: Read filenames from jar-file
    843804 Newbie
    Currently Being Moderated
    AndrewThompson64 wrote:
    Lisa_R wrote:
    ..I tried to remove the URLDecoder, but the result was the same. I still get:
    "http:\server:8080\test\client.jar The filename, directory name, or volume label syntax is incorrect".
    Given an URL should use '/' as opposed to '\', and starts with 2 '/' rather than one, it is not surprising that URL was rejected. Strange as it may seem, programming by typing random characters into an editor will rarely, if ever, work to create a working application.
    Yes, I agre that it´s not that surprising that the URL was rejected (even though the URL work in firefox and IE). But I didn´t write it that way. The code that put the URL together worked fine in previous versions of the jdk.

    >
    ..The reason why I want to read the filenames from the jar instead of including a list of the names is that if you
    want to add an image then all you have to do is include it in the directory..
    That is largely irrelevant, since the Jar needs to be freshly built, uploaded by the developer, and downloaded by the client. Since all of that needs to be done for the addition of a single image, you might as well add an Ant task to the build that provides a single file (like I suggested earlier) that lists the resources of interest. It would take less time to write an Ant task, than debug all the hoops the code is currently jumping through(1) in order to get the information of interest.
    If this was the only place were this method was used I would agre with you, but unfortunatelly it´s not. The application dosen´t only read image filenames it also reads class-names.
    1) And even if you got that working, it would be likely that Sun would introduce some change in the next version of Java that breaks it again. My advice is to stop fighting it, and go with the flow.
    Thats not good of course! But it has been working for a long time, so I think I will try to make it work again....
    Is there someone out there who has done this or knows how to do it I would appreciate some help! :-)