0

The jar files are generated during build time and contain xml files that need to be read during run time. I am using Java webstart which downloads these jar file to a local folder. the local path to where the jar is stored is: C:\Users\username\Desktop\Project\jars

This folder contains multiple jar files. What i am trying to achieve is for the code to locate a particular jar based on a filename it contains and read an xml file contained in the jar. The xml file is located in a folder called xmlfiles in the jar but this is returning null.

Note: this used to work with previous versions of Java 3-5 but after our migration to Java 7, this method returns null. This is the portion of the method throwing the exception

private InputStream Jar(String _filename) throws  IOException
    {
      String path = "xmlfiles/" + _filename;
      InputStream content = Thread.currentThread().getContextClassLoader().getResourceAsStream(path) 
        System.out.println("loaded xml);

    }

I have also tried getting the URL and printing out the string, but get a fileNotfound Exception

URL path = Thread.currentThread().getContextClassLoader().getResource(url);

I have tried various solutions with similar question on here but none of the solution can locate the file (returnin null or file not found) InputStream from jar-File returns always null Parse XML File within Jar Accessing a file inside a .jar file

Community
  • 1
  • 1
  • Are these jars in your classpath at runtime? – fge May 15 '14 at 13:39
  • @fge yes they are in the classpath – user3581872 May 15 '14 at 13:40
  • Well then simply do a resource lookup, no need to name the jar; or do you have files with the same path in different jars? – fge May 15 '14 at 13:48
  • @fge The path is the same for all the generated jars but the filename is different based on _filename – user3581872 May 15 '14 at 14:35
  • That is a very bad idea! Basically this means you cannot use a resource lookup but have to walk all the classloader URLs and find the suitable jar before opening it and fetching the file... Make your life more simple: have the path be different for all your resources; then a simple resource lookup will be enough. – fge May 15 '14 at 14:57

0 Answers0