javaubuntujava-21

serialver tool in openjdk 21 does not work for nested classes


I have created a demo class as follows:

public class Demo2 implements Serializable {
    int id;

    public static void main(String[] args) {
        System.out.println("Hi");
    }
}

When I execute the command:

serialver com.sample.Demo2

it provides the correct result:

com.sample.Demo2:    private static final long serialVersionUID = -3814645723313276333L;

For the second class:

public class Demo {
    static class Serial implements Serializable {
        int id;
    }

    public static void main(String[] args) {
        System.out.println("Hi");
    }
}

When I execute the command:

serialver com.sample.Demo$Serial

I get following output:

Class com.sample.Demo is not Serializable.

I have OpenJDK 21.0.7 installed on my Ubuntu machine:

OpenJDK version

Is there any way, this can be resolved?


Solution

  • The $ character has special meaning in Bash (and other shell variants, AFAIK). To use it literally, you must either escape it with \, or you need to enclose the value in single quotes (not double quotes).

    That is, you need to use

    serialver com.sample.Demo\$Serial
    

    or

    serialver 'com.sample.Demo$Serial'
    

    See also Quoting in the Bash Reference Manual.

    Alternatively, serialver also accepts the class name as used within Java code, i.e. com.sample.Demo.Serial, instead of the mangled name used internally.

    So, you can also use

    serialver com.sample.Demo.Serial
    

    This removes the need for escaping.