3 Replies Latest reply: Oct 2, 2008 11:02 AM by 843804 RSS

    class.getResource("/lib/file.xml") does not work after JRE 1.5.0_16 update

    843804
      My java App is deployed via Webstart. My company has updated the JRE to JRE 1.5.0_16 and now i get the following Exception:
      SPLASH: Komponenten initialisieren...(20)
      
      02.10.2008 10:58:58 java.util.prefs.WindowsPreferences <init>
      
      WARNUNG: Could not open/create prefs root node Software\JavaSoft\Prefs
      at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
      
      java.net.MalformedURLException: no !/ in spec
      java.lang.NullPointerException
      at limbus.apps.seminareditor.models.ComboBoxModel_Status.loadData(ComboBoxModel_Status.java:47)
      This is the Code, where the Error occures...
              try{
                  SAXBuilder builder = new SAXBuilder();
                  doc = builder.build(ComboBoxModel_Status.class.getResource("/bin/editor-cbo-status.xml"));
                  
              }catch (org.jdom.JDOMException e) {
                  System.out.println(e.getMessage());
                  e.printStackTrace();
              }catch(java.io.IOException e){
                  System.err.println(e);
              }
      Under the previous JRE 1.5.0_xx the Application runs very well.... Any ideas, or solution desired...
        • 1. Re: class.getResource("/lib/file.xml") does not work after JRE 1.5.0_16 update
          843804
          Hmmm, i have found this Bug in Suns Bugdatabase. That seems to fit to my Problem...

          [http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6753651]

          Now i check out, if there is an fix for this...
          • 2. Re: class.getResource("/lib/file.xml") does not work after JRE 1.5.0_16 update
            843804
            Okay, there seems to be a Workaround:

            [http://www.objectdefinitions.com/odblog/2008/workaround-for-bug-id-6753651-find-path-to-jar-in-cache-under-webstart/]

            But i have no idea how to use this...
            /**
                 * This method will return the path to the jar file containing the resource which this URL references
                 *
                 * It should work with URLs returned by class.getResource() under java 1.5.0_16
                 * and 1.6.0_07, as well as maintaining backwards compatibility with previous jres
                 *
                 * The two jre above contain security patches which make the file path of the jar
                 * inaccessible under webstart. This patch works around that by using reflection to access
                 * private fields in the webstart.jar where required. This will only work for signed webstart
                 * apps running with all security permissions
                 *
                 * @param jarUrl - url which has jar as the protocol
                 * @return path to Jar file for this jarURL
                 */
                private static String getJarFilePath(URL jarUrl) {
                    JarFile jarFile = getJarFile(jarUrl);
                    return findJarPath(jarFile);
                }
            
                public static JarFile getJarFile(URL jarUrl) {
                    try {
                        JarURLConnection jarUrlConnection = (JarURLConnection)jarUrl.openConnection();
            
                        //try the getJarFile method first.
                        //Under webstart in 1.5.0_16 this is overriden to return null
                        JarFile jarFile = jarUrlConnection.getJarFile();
            
                        if ( jarFile == null) {
                            jarFile = getJarFileByReflection((JNLPCachedJarURLConnection)jarUrlConnection);
                        }
                        return jarFile;
                    } catch (Throwable t) {
                        throw new RuntimeException("Failed to get JarFile from jarUrlConnection", t);
                    }
                }
            
                private static JarFile getJarFileByReflection(JNLPCachedJarURLConnection jarUrlConnection) throws NoSuchFieldException, IOException, IllegalAccessException {
                    Class jnlpConnectionClass = JNLPCachedJarURLConnection.class;
                    Field jarFileField;
                    try {
                        jarFileField = jnlpConnectionClass.getDeclaredField("jarFile");
                    } catch ( Throwable t) {
                        jarFileField = jnlpConnectionClass.getDeclaredField("_jarFile");
                    }
                    jarUrlConnection.connect(); //this causes the connection to set the jarFile field
                    jarFileField.setAccessible(true);
                    return (JarFile)jarFileField.get(jarUrlConnection);
                }
            
                private static String findJarPath(JarFile cachedJarFile) {
                    try {
                        String name = cachedJarFile.getName();
            
                        //getName is overridden to return "" under 1.6.0_7 so use reflection
                        if ( name == null || name.trim().equals("")) {
                            Class c = ZipFile.class;
                            Field field = c.getDeclaredField("name");
                            field.setAccessible(true);
                            name = (String)field.get(cachedJarFile);
                        }
                        return name;
                    } catch (Throwable t) {
                        throw new RuntimeException("Failed to get find name from jarFile", t);
                    }
                }
            • 3. Re: class.getResource("/lib/file.xml") does not work after JRE 1.5.0_16 update
              843804
              Here is another Thread about this Problem... [http://forums.sun.com/thread.jspa?forumID=38&threadID=5319615]