Home > Net >  java.lang.ClassNotFoundException: com/sun/security/cert/internal/x509/X509V1CertImpl exception in Ja
java.lang.ClassNotFoundException: com/sun/security/cert/internal/x509/X509V1CertImpl exception in Ja

Time:12-06

In Java 17.0.5, com.sun.security.cert.internal.x509.X509V1CertImpl has been removed. And if I call X509Certificate.getInst() with X509Provider=null,
than className = "com.sun.security.cert.internal.x509.X509V1CertImpl" and in the lower part it call Class<?> certClass = Class.forName(className); and end up

java.lang.ClassNotFoundException: com/sun/security/cert/internal/x509/X509V1CertImpl

private static final X509Certificate getInst(Object value) throws CertificateException {
        String className = X509Provider;
        if (className == null || className.isEmpty()) {
            className = "com.sun.security.cert.internal.x509.X509V1CertImpl";
        }

        try {
            Class<?>[] params = null;
            if (value instanceof InputStream) {
                params = new Class[]{InputStream.class};
            } else {
                if (!(value instanceof byte[])) {
                    throw new CertificateException("Unsupported argument type");
                }

                params = new Class[]{value.getClass()};
            }

            Class<?> certClass = Class.forName(className);
            Constructor<?> cons = certClass.getConstructor(params);
            Object obj = cons.newInstance(value);
            return (X509Certificate)obj;
        } catch (ClassNotFoundException var6) {
            throw new CertificateException("Could not find class: "   var6);

Is this a bug for Java 17.0.5?

CodePudding user response:

You're relying on an internal class from the core libraries. That's an inherently dangerous thing to do as these can change or disappear without prior warning even in between minor releases.

In this case, the class you're relying on extends another class that was deprecated in Java 9 for later removal. This removal most likely happened in Java 17 (though it may have been anywhere post Java 11).

So you had ample warning it was going to disappear. This is not a bug indeed.

CodePudding user response:

COPY FROM https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/security/cert/X509Certificate.html

----REF START----

Certificates are instantiated using a certificate factory. The following is an example of how to instantiate an X.509 certificate:

 try (InputStream inStream = new FileInputStream("fileName-of-cert")) {
     CertificateFactory cf = CertificateFactory.getInstance("X.509");
     X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
 }

----REF END----

  •  Tags:  
  • java
  • Related