Skip to content

XMLDecoder in native image fails with ArrayIndexOutOfBoundsException #10509

@paddy-axio

Description

@paddy-axio

Describe the Issue

When trying to deserialize an XML representation of a Java Bean using java.beans.XMLDecoder, the native image fails with an ArrayIndexOutOfBoundsException.
The same code works well when packaged as a runnable Jar. However, it fails when run as a native image. I have also tried to include the bean class (test.Person) in reflect-config and also serialization-config hints.

Using the latest version of GraalVM can resolve many issues.

GraalVM Version

Oracle GraalVM 21+35.1 (build 21+35-jvmci-23.1-b15)

Operating System and Version

Darwin Kernel Version 23.0.0

Troubleshooting Confirmation

Run Command

./App

Expected Behavior

Person object is constructed successfully and the below string is printed

Hello from Person{firstName='John', lastName='Doe', age=50}

Actual Behavior

Running the native App results in error as below

java.lang.InstantiationException: com.sun.beans.decoder.JavaElementHandler
Continuing ...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
        at java.desktop@21/java.beans.XMLDecoder.readObject(XMLDecoder.java:253)
        at test.XmlDecoderTest.decodeTestPerson(XmlDecoderTest.java:13)
        at test.XmlDecoderTest.main(XmlDecoderTest.java:8)
        at java.base@21/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)

Steps to Reproduce

XmlDecoderTest.java

package test;

import java.beans.XMLDecoder;
import java.io.ByteArrayInputStream;

public class XmlDecoderTest {
    public static void main(String[] args) {
        System.out.println(decodeTestPerson());
    }

    public static String decodeTestPerson() {
        XMLDecoder decoder = new XMLDecoder(new ByteArrayInputStream(getXmlEncodedPerson().getBytes()));
        Object readObject = decoder.readObject();
        Person decodedPerson = (Person) readObject;
        return "Hello from " + decodedPerson;
    }

    private static String getXmlEncodedPerson() {
        return """
                <?xml version="1.0" encoding="UTF-8"?>
                <java version="21.0.1" class="java.beans.XMLDecoder">
                 <object class="test.Person">
                  <void property="age">
                   <int>50</int>
                  </void>
                  <void property="firstName">
                   <string>John</string>
                  </void>
                  <void property="lastName">
                   <string>Doe</string>
                  </void>
                 </object>
                </java>
                """;
    }
}

Person.java

package test;

import java.io.Serializable;

public class Person implements Serializable {
    private String firstName;
    private String lastName;
    private Integer age;

    @Override
    public String toString() {
        return "Person{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", age=" + age +
                '}';
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

** Build **
javac -d build test/XmlDecoderTest.java
jar --create --file App.jar --main-class test.XmlDecoderTest -C build .
native-image -jar App.jar

** Test jar **
java -jar App.jar

** Test native-image **
./App

Additional Context

No response

Run-Time Log Output and Error Messages

java.lang.InstantiationException: com.sun.beans.decoder.JavaElementHandler
Continuing ...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
at java.desktop@21/java.beans.XMLDecoder.readObject(XMLDecoder.java:253)
at test.XmlDecoderTest.decodeTestPerson(XmlDecoderTest.java:13)
at test.XmlDecoderTest.main(XmlDecoderTest.java:8)
at java.base@21/java.lang.invoke.LambdaForm$DMH/sa346b79c.invokeStaticInit(LambdaForm$DMH)

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions