This discussion is archived
7 Replies Latest reply: Dec 22, 2011 9:58 AM by 907078 RSS

NullPointer Exception ,web start Static class loading in sun JRE with JNLP

825655 Newbie
Currently Being Moderated
I have a netbenas based application and using with jre 1.6.0_22.
i am getting NPE as
java.lang.NullPointerException
at com.sun.deploy.security.CPCallbackHandler.isAuthenticated(Unknown Source)
at com.sun.deploy.security.CPCallbackHandler.access$1300(Unknown Source)
at com.sun.deploy.security.CPCallbackHandler$ChildElement.checkResource(Unknown Source)
at com.sun.deploy.security.DeployURLClassPath$JarLoader.checkResource(Unknown Source)
at com.sun.deploy.security.DeployURLClassPath$JarLoader.getResource(Unknown Source)
at com.sun.deploy.security.DeployURLClassPath.getResource(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at com.sun.jnlp.JNLPClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.osi.solutions.platform.classbuilder.view.ClassExplorerTopComponent.<init>(ClassExplorerTopComponent.java:89)
at com.osi.solutions.platform.classbuilder.view.ClassExplorerTopComponent.getDefault(ClassExplorerTopComponent.java:143)
at com.osi.solutions.platform.classbuilder.view.ClassExplorerTopComponent$ResolvableHelper.readResolve(ClassExplorerTopComponent.java:198)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadResolve(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at org.netbeans.modules.settings.convertors.XMLSettingsSupport$SettingsRecognizer.readSerial(XMLSettingsSupport.java:544)
at org.netbeans.modules.settings.convertors.XMLSettingsSupport$SettingsRecognizer.instanceCreate(XMLSettingsSupport.java:576)
at org.netbeans.modules.settings.convertors.SerialDataConvertor$SettingsInstance.instanceCreate(SerialDataConvertor.java:420)
at org.netbeans.core.windows.persistence.PersistenceManager.getTopComponentPersistentForID(PersistenceManager.java:531)
at org.netbeans.core.windows.persistence.PersistenceManager.getTopComponentForID(PersistenceManager.java:641)
at org.netbeans.core.windows.PersistenceHandler.getTopComponentForID(PersistenceHandler.java:422)
at org.netbeans.core.windows.PersistenceHandler.load(PersistenceHandler.java:147)
at org.netbeans.core.windows.WindowSystemImpl.load(WindowSystemImpl.java:69)
[catch] at org.netbeans.core.NonGui$2.run(NonGui.java:178)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:104)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

This issue comes very occassionaly .
Does someone have any idea what is wrong. Is this related to any jre bug.

Thanks,
totaram
  • 1. Re: NullPointer Exception ,web start Static class loading in sun JRE with JNLP
    645533 Newbie
    Currently Being Moderated
    This problem is also comming wiht jre 1.6.23.
    Does anybody has any clue regarding this.

    Regards,
    Raj..
  • 2. Re: NullPointer Exception ,web start Static class loading in sun JRE with JNLP
    827523 Newbie
    Currently Being Moderated
    I'm also facing the same problem : same stacktrace , NPE while loading a class in a jnlp context
    I'm running a jnlp based applet, with jre 1.6.0_22 and 1.6.0_23, in iE8.

    2 trails to follow :
    - may be related to the lazy download of my jars
    - or related to the "jnlp.concurrentDownloads=15" property (see [http://download.oracle.com/javase/6/docs/technotes/guides/jweb/riaJnlpProps.html] )

    that was the last 2 majors modifications I made.

    --
    Julien
  • 3. Re: NullPointer Exception ,web start Static class loading in sun JRE with JNLP
    834450 Newbie
    Currently Being Moderated
    I have the same problem, has anybody found anything?
    I suspect it's related to JNLP applications created by NetBeans.
  • 4. Re: NullPointer Exception ,web start Static class loading in sun JRE with JNLP
    834450 Newbie
    Currently Being Moderated
    Someone found the problem, and it fixed my problem too
    http://bugs.sun.com/view_bug.do?bug_id=6967414

    increase your initial heap size
  • 5. Re: NullPointer Exception ,web start Static class loading in sun JRE with JNLP
    squaat Newbie
    Currently Being Moderated
    Hi I'm the one who posted those findings... though further investigation found that increasing your heap size can help, but at best it only reduces the occurances of the problem.

    The real cause of the issue is that the Signer's info for a CachedJarFile is held onto by softreferences and is not "rebuilt" if it has been garbage collected. the Reason that increasing your initial heap size works, is that it helps to delay the conditions on which these softreferences are garbage collected. (SoftReferences become eligible for garbage collection when the heap needs to be expanded)

    I've put together a hack that traverses all of the jars in a webstart application, finds their corresponding CachedJarFile instance and sticks all of the relevant SoftReferences into a static list, so that they become "hard references" and are never garbage collected.

    Below is my JarSignersHardLinker.java hack To use it, just call JarSignersHardLinker.go() it will then
    * Check that you are running on webstart and you are on java 1.6 update 19 or higher.
    * If the above is true then it will spawn a new thread and create hard links to all the jarsigners for each jar on the classpath.

    If you need more info email me on my gmail account. My user name is squaat. I've also posted this code at Re: Error with Java WebStart Signed Jars on 1.6.0_19's new Mixed  Code

    If you find this helpful and it solves your problems, please leave a positive comment and/or vote for the bug at:

    http://bugs.sun.com/view_bug.do?bug_id=6967414

    If any oracle/sun webstart engineers are reading this, please contact me... we'd really like this bug fixed.
    import java.io.IOException;
    import java.lang.ref.SoftReference;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    import java.net.JarURLConnection;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.LinkedHashSet;
    import java.util.List;
    import java.util.Set;
    import java.util.jar.JarFile;
     
     
    /**
     * A utility class for working around the java webstart jar signing/security bug 
     * 
     * see http://bugs.sun.com/view_bug.do?bug_id=6967414 and http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6805618
     * @author Scott Chan
      */
    public class JarSignersHardLinker {
        
        private static final String JRE_1_6_0 = "1.6.0_";
        
        /**
         * the 1.6.0 update where this problem first occurred
         */
        private static final int PROBLEM_JRE_UPDATE = 19;
        
        public static final List sm_hardRefs = new ArrayList();
        
        protected static void makeHardSignersRef(JarFile jar) throws java.io.IOException { 
            
            System.out.println("Making hard refs for: " + jar );
            
            if(jar != null && jar.getClass().getName().equals("com.sun.deploy.cache.CachedJarFile")) {
     
                 //lets attempt to get at the each of the soft links.
                 //first neet to call the relevant no-arg method to ensure that the soft ref is populated
                 //then we access the private member, resolve the softlink and throw it in a static list.
                
                callNoArgMethod("getSigners", jar);
                makeHardLink("signersRef", jar);
                
                callNoArgMethod("getSignerMap", jar);
                makeHardLink("signerMapRef", jar);
                
    //            callNoArgMethod("getCodeSources", jar);
    //            makeHardLink("codeSourcesRef", jar);
                
                callNoArgMethod("getCodeSourceCache", jar);
                makeHardLink("codeSourceCacheRef", jar);
            }            
        }
        
        
        /**
         * if the specified field for the given instance is a Softreference
         * That soft reference is resolved and the returned ref is stored in a static list,
         * making it a hard link that should never be garbage collected
         * @param fieldName
         * @param instance
         */
        private static void makeHardLink(String fieldName, Object instance) {
            
            System.out.println("attempting hard ref to " + instance.getClass().getName() + "." + fieldName);
            
            try {
                Field signersRef = instance.getClass().getDeclaredField(fieldName);
                
                signersRef.setAccessible(true);
                
                Object o = signersRef.get(instance);
                
                if(o instanceof SoftReference) {
                    SoftReference r = (SoftReference) o;
                    Object o2 = r.get();
                    sm_hardRefs.add(o2);
                } else {
                    System.out.println("noooo!");
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
                return;
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        
        /**
         * Call the given no-arg method on the given instance
         * @param methodName
         * @param instance
         */
        private static void callNoArgMethod(String methodName, Object instance) {
            System.out.println("calling noarg method hard ref to " + instance.getClass().getName() + "." + methodName + "()");
            try {
                Method m = instance.getClass().getDeclaredMethod(methodName);
                m.setAccessible(true);
                
                m.invoke(instance);
     
            } catch (SecurityException e1) {
                e1.printStackTrace();
            } catch (NoSuchMethodException e1) {
                e1.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
     
        
        /**
         * is the preloader enabled. ie: will the preloader run in the current environment
         * @return
         */
        public static boolean isHardLinkerEnabled() {
             
             boolean isHardLinkerDisabled = false;  //change this to use whatever mechanism you use to enable or disable the preloader
            
            return !isHardLinkerDisabled && isRunningOnJre1_6_0_19OrHigher() && isRunningOnWebstart();
        }
        
        /**
         * is the application currently running on webstart
         * 
         * detect the presence of a JNLPclassloader
         * 
         * @return
         */
        public static boolean isRunningOnWebstart() {
            ClassLoader cl = Thread.currentThread().getContextClassLoader();
            
            while(cl != null) {
                if(cl.getClass().getName().equals("com.sun.jnlp.JNLPClassLoader")) {
                    return true;
                }
                cl = cl.getParent();
            }
            
            return false;
     
        }
        
        /**
         * Is the JRE 1.6.0_19 or higher?
         * @return
         */
        public static boolean isRunningOnJre1_6_0_19OrHigher() {
            String javaVersion = System.getProperty("java.version");
            
            if(javaVersion.startsWith(JRE_1_6_0)) {
                //then lets figure out what update we are on
                String updateStr = javaVersion.substring(JRE_1_6_0.length());
                
                try {
                    return Integer.parseInt(updateStr) >= PROBLEM_JRE_UPDATE;
                } catch (NumberFormatException e) {
                    //then unable to determine updatedate level
                    return false;
                }
            } 
            
            //all other cases
            return false;
            
        }
        
        
         /**
          * get all the JarFile objects for all of the jars in the classpath
          * @return
          */
         public static Set<JarFile> getAllJarsFilesInClassPath() {
         
              Set<JarFile> jars = new LinkedHashSet<JarFile> (); 
             
             for (URL url : getAllJarUrls()) {
                 try {
                     jars.add(getJarFile(url));
                 } catch(IOException e) {
                      System.out.println("unable to retrieve jar at URL: " + url);
                 }
             }
             
             return jars;
         }
         
        /**
         * Returns set of URLS for the jars in the classpath.
         * URLS will have the protocol of jar eg: jar:http://HOST/PATH/JARNAME.jar!/META-INF/MANIFEST.MF
         */
        static Set<URL> getAllJarUrls() {
            try {
                Set<URL> urls = new LinkedHashSet<URL>();
                Enumeration<URL> mfUrls = Thread.currentThread().getContextClassLoader().getResources("META-INF/MANIFEST.MF");
                while(mfUrls.hasMoreElements()) {
                    URL jarUrl = mfUrls.nextElement();
    //                System.out.println(jarUrl);
                    if(!jarUrl.getProtocol().equals("jar")) continue;
                    urls.add(jarUrl);
                }
                return urls;
            } catch(IOException e) {
                throw new RuntimeException(e);
            }
        }
        
        /**
         * get the jarFile object for the given url
         * @param jarUrl
         * @return
         * @throws IOException
         */
        public static JarFile getJarFile(URL jarUrl) throws IOException {
            URLConnection urlConnnection = jarUrl.openConnection();
            if(urlConnnection instanceof JarURLConnection) {
                // Using a JarURLConnection will load the JAR from the cache when using Webstart 1.6
                // In Webstart 1.5, the URL will point to the cached JAR on the local filesystem
                JarURLConnection jcon = (JarURLConnection) urlConnnection;
                return jcon.getJarFile();
            } else {
                throw new AssertionError("Expected JarURLConnection");
            }
        }
        
        
        /**
         * Spawn a new thread to run through each jar in the classpath and create a hardlink
         * to the jars softly referenced signers infomation.
         */
        public static void go() {
            if(!isHardLinkerEnabled()) {
                return;
            }
            
            System.out.println("Starting Resource Preloader Hardlinker");
            
            Thread t = new Thread(new Runnable() {
     
                public void run() {
                    
                    try {
                        Set<JarFile> jars = getAllJarsFilesInClassPath();
                        
                        for (JarFile jar : jars) {
                            makeHardSignersRef(jar);
                        }
     
                    } catch (Exception e) {
                        System.out.println("Problem preloading resources");
                        e.printStackTrace();
                    } catch (Error e) {
                         System.out.println("Error preloading resources");
                         e.printStackTrace();
                    }
                }
                
            });
            
            t.start();
            
        }
    }
    Edited by: 855200 on 04-Jul-2011 17:31
  • 6. Re: NullPointer Exception ,web start Static class loading in sun JRE with JNLP
    894486 Newbie
    Currently Being Moderated
    Hi !



    I have seen your post on Oracle forum to solve following problem:

    http://bugs.sun.com/view_bug.do?bug_id=6967414


    I try to use JarSignersHardLinker class. It seems to work but after calling JarSignersHardLinker.go(), I have the following error. Have I lost something in the classpath ??



    org.springframework.beans.factory.BeanCreationException : Error creating bean with name 'monitoringService' defined in class path resource [META-INF/appCxt-client.xml]: Invocation of init method failed; nested exception is java.lang.NullPointerException
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
    org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
    org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
    org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
    org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
    org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
    org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
    org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
    fr.alcatel.ecsm.ds.util.access.ServiceFactoryManager.(ServiceFactoryManager.java:86)
    fr.alcatel.ecsm.ds.util.access.ServiceFactoryManager.getInstance(ServiceFactoryManager.java:111)
    fr.alcatel.ecsm.gui.Gui$2.exec(Gui.java:554)
    fr.alcatel.ecsm.common.splash.SplashProgressBar.execTasks(SplashProgressBar.java:348)
    fr.alcatel.ecsm.common.splash.SplashProgressBar$1.construct(SplashProgressBar.java:139)
    fr.alcatel.paloma.gui.dialog.SwingWorker$2.run(SwingWorker.java:166)
    java.lang.Thread.run(Unknown Source)



    Hope you can help me …


    Thank you for your work!
  • 7. Re: NullPointer Exception ,web start Static class loading in sun JRE with JNLP
    907078 Newbie
    Currently Being Moderated
    Thanks for your solution, squaat. I have come across this bug in the context of an Applet, so I think the scope of affected ClassLoaders may be the URLClassLoader. So I will be trying this with a isRunningFromURL predicate which modifies the isRunningOnWebStart predicate to search for java.net.URLClassLoader instead of com.sun.jnlp.JNLPClassLoader.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points