0

Hy

I try to connect a java program to an REST API.

With the same part of code I have a Java Exception in Java 6 and it works fine in Java 8.

It's the same environment :

  • trustore
  • machine
  • unix user

the code :

import java.io.DataInputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class MainClass {

    public static void main (String[] args){

        String serviceUrl = "https://api.domain.com" + "/endpont/path";
        try {
            URL url = new URL(serviceUrl);
            URLConnection connection = null;
            try{
                connection = url.openConnection();
                connection.setRequestProperty("Accept", "application/json");
                connection.setRequestProperty("Content-Type", "application/json");
                String body = "";
                String inputLine;
                DataInputStream input = new DataInputStream (connection.getInputStream());
                while (((inputLine = input.readLine()) != null)){
                    body += inputLine;
                }
                System.out.println(body);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}

the error in Java 6 : sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Somebody know how it's different ? Can I use some tricks to have the same result in Java 6 ?

The CN of the cert is a wildcard : "*.domain.com" . It can be the cause ?

I tried several api but there all used the sun SSL layer. Do you know an other to replace it ?

Antoine
  • 13
  • 3
  • follow the link Step by Step : http://www.mkyong.com/webservices/jax-ws/suncertpathbuilderexception-unable-to-find-valid-certification-path-to-requested-target/ – Shubham Mar 18 '20 at 08:59
  • 1
    Just one of many reasons NOT to use Java 6 anymore. The last public release of Java 6 was 7 years ago. – Stephen C Mar 18 '20 at 09:06
  • I'm agree with you, but have I not the choice of the java version on the project. – Antoine Mar 18 '20 at 10:01

1 Answers1

0

JRE has it's own keystore, where certificates can be stored. Maybe your JDK/JRE for Java 6 has different keys than Java 8.

Šimon Kocúrek
  • 1,591
  • 1
  • 12
  • 22