Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.io.Closeable #108

Open
ashishbhat opened this issue Mar 18, 2024 · 3 comments

Comments

@ashishbhat
Copy link

I have created an Ant XJC task and I am using jaxb-xjc-4.0.4. While building I am seeing below error:
java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.io.Closeable (jdk.internal.loader.ClassLoaders$AppClassLoader and java.io.Closeable are in module java.base of loader 'bootstrap')

@bond4u
Copy link

bond4u commented Jun 4, 2024

I have this happening with ant 1.10.12 and jaxb-ri 4.0.3. Looking at ProtectedTask class, it seems that it tries to close classloader without checking that classloader implements Closeable.

@abcoates
Copy link

abcoates commented Jun 7, 2024

Repeating what I wrote in #107:

In com.sun.istack.tools.ProtectedTask, lines 87-94, the code is

    while (cl != null && !ccl.equals(cl)) {
        try {
            ((Closeable) cl).close();
        } catch (IOException ex) {
            throw new BuildException(ex);
        }
        cl = getParentClassLoader(cl);
    }

The problem is that on line 89 'cl' is cast to 'Closeable' without checking whether it is an instance of 'Closeable' or not. In JDK8, classloaders were invariably a type of URLClassLoader, which is 'Closeable', but in JDK9+ this is no longer a valid assumption, causing a ClassCastException at line 89.

This issue affects anyone using the XJC task in Apache Ant who is trying to move from JDK8 to JDK9+. The fix is to change the lines 87-89 from

    while (cl != null && !ccl.equals(cl)) {
        try {
            ((Closeable) cl).close();

to

    while (cl != null && !ccl.equals(cl) && (cl instanceof Closeable)) {
        try {
            ((Closeable) cl).close();

I'm personally held up on a project by this issue, so would be enormously grateful to see this fix released. Thanks very much in advance for your help.

@tienttitedu
Copy link

Hi all, I got the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants