2

Upgrading to JDK 1.7 update 45, from 21, running in JBoss AS 7.2.0.Final (or EAP 6.1.0.Alpha1 (AS 7.2.0.Alpha1-redhat-4)) I am getting a NullPointerException when initialising ImageIO. Which is being used for image resizing during uploads.

IIORegistry is called as part of the ImageIO initialisation but it NPEs at this code:

public static IIORegistry getDefaultInstance() {
    AppContext context = AppContext.getAppContext(); // <- context==null here??

However it doesn't have a problem when running outside JBoss (as a JUnit test for example).

I'm not sure when this behaviour changed but it was somewhere between JDK update 21 and 45 (from hints online it was probably update 25).

There are many similar (but not identical) tales of woe out there but none of the suggested solutions works here. They also relate to other servers than JBoss (WebSphere for example). So this appears to be a more generic container configuration problem.

I am running with both these (and the variations of using them or not):

-Djava.awt.headless=true
-Dawt.toolkit=sun.awt.HToolkit

JBoss imports the javax.imageio.* packages by default. For evidence of that, see this which mentions javax.api, which refers to this module:

modules/system/layers/base/javax/api/main/module.xml

I've also tried adding them to jboss-deployment-structure.xml explicitly:

    <system>
        <paths>
            <path name="javax/imageio"/>
            <path name="javax/imageio/event"/>
            <path name="javax/imageio/metadata"/>
            <path name="javax/imageio/plugins/bmp"/>
            <path name="javax/imageio/plugins/jpeg"/>
            <path name="javax/imageio/spi"/>
            <path name="javax/imageio/stream"/>
        </paths>
    </system>

Note (for anyone else searching for this) also that when you first trigger this error you see this:

java.lang.NullPointerException
    javax.imageio.spi.IIORegistry.getDefaultInstance(IIORegistry.java:155)

But on subsequent tries, without a server restart, you see this:

java.lang.NoClassDefFoundError: Could not initialize class javax.imageio.ImageIO

Which is misleading - the class is available to the classloader but has been unloaded due to the first failure. So remember to restart the server before testing again.

It seems that something is preventing AppContext.getAppContext() returning anything useful but currently I am at a loss as to what magic bit of config that is!

RedYeti
  • 1,024
  • 14
  • 28
  • `NoClassDefFoundError` is a normal situation if the class failed to initialize it's static fields (class initialization only happens once). But I agree it's confusing. Also, are you sure `com.sun.imageio.*` is the package you are looking for? ImageIO is in `javax.imageio`. – Harald K Dec 16 '13 at 13:02
  • @haraldK Thanks! Yes silly oversight created by copy/pasting that XML config from a forum somewhere. I've now corrected that in the question text since, sadly, it's the same situation (those classes are imported by default). Which isn't really surprising since it's not the class isn't being loaded (I thought I remembered that the class loader evicts classes which fail to init - thanks for confirming that too). Still at a loss as to why the AppContext is null when it clearly wasn't in the earlier JDK update release... – RedYeti Dec 16 '13 at 13:52
  • `AppContext.getAppContext()` seems to appear in a lot of bugs all over, since u25.. Not sure what the reason is, but hoping Oracle will look into it. I've seen workarounds resorting to setting the appcontext explicitly using reflection and `setAccessible(true)`, but not sure if that's a good idea... – Harald K Dec 16 '13 at 21:27
  • Yes thanks @haraldK - I'm now thinking you're right and it is a Java bug :( Will probably run on OpenJDK for now and go back to Oracle's version once they fix it. Though I'm still a little perplexed that I can get it running in a JUnit but not in JBoss... – RedYeti Dec 17 '13 at 11:17
  • Could you fix this somehow? I have the same exact problem in a Tomcat web app and I don't have idea what can I do to fix this. – Agustin Lopez Jan 19 '14 at 05:19
  • OpenJDK fixes the problem but I wonder if this is a bug or there is something else behind all this... I would like to use Oracle's version. – Agustin Lopez Jan 19 '14 at 05:23
  • @AgustinLopez Worked around by sticking with update 21. We'll review it once 1.7 update 66 is out (which should fix it...). Surprised that using Open JDK worked for you since I didn't think the bug was fixed there until update 66 either! – RedYeti Jan 20 '14 at 15:38

2 Answers2

1

Well u60 still has this problem. I think we're still waiting for a fix from Oracle.

However, for my particular problem the neat work-around suggested here worked just fine:

Could not initialize class javax.imageio.ImageIO

From which I just took this part:

if(AppContext.getAppContext() == null) {
    SunToolkit.createNewAppContext();
}

Since the code I use it within is called infrequently, and doing a null check is cheap enough that I don't think it warrants the complexity of using the whole ApplicationListener solution.

Community
  • 1
  • 1
RedYeti
  • 1,024
  • 14
  • 28
0

I am facing the same problem and I am using the solution mentioned here as a temporary workaround: https://forums.oracle.com/message/11082162#11082162. That does work for me.

Hope that oracle comes up with a fix sooner.

  • Although it's a tedious process.. I really think everyone running into this issue should file a bug report with Oracle or vote for existing bugs in their bug tracking system. While no guarantee that will help in getting a fix, at least it does not hurt to make Oracle aware how big this issue is. – Harald K Dec 19 '13 at 09:26
  • Which is their bug tracking system? – Agustin Lopez Jan 19 '14 at 18:00