Home > Mobile >  PCKS12 NoSuchAlgorithmException Error while compiling
PCKS12 NoSuchAlgorithmException Error while compiling

Time:09-26

I'm just trying P12 certificate import in java code for one project testing. I have tested the certificate with my python code and it is working fine. Below is my Java sample code which i'm trying to compile. But it is throwing error. I'm new to java thats why getting confused with the error message.

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.FileInputStream;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
//import org.json.JSONObject;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.security.KeyStore;
import java.security.SecureRandom;
import java.util.logging.Logger;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;



public class APIClient{
    public static void main(String[] args) throws IOException{

        int responseCode = GetDetails();
        System.out.println(responseCode);
        if (responseCode == 200){
            System.out.println("yes success");
        }
        else{
            System.out.println("no failed");
        }
    }
    public static int GetDetails() throws  IOException{
        String CLIENT_KEYSTORE_TYPE = "PKCS12";
        String clientPassword = "xxxxxxxxxx";
        String clientCertificatePath = "Client_Certificate.p12";
        KeyStore keyStore = KeyStore.getInstance(CLIENT_KEYSTORE_TYPE);
        keyStore.load(null, clientPassword.toCharArray());
        URL url = new URL("https://localhost");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        int responseCode = connection.getResponseCode();
        return responseCode;
    }
}

Below is the error while compiling with javac. Also i have added the P12 certificate in same directory where the APIClient.java located.

javac APIClient.java

Error below while compiling with above javac command

APIClient.java:39: error: unreported exception KeyStoreException; must be caught or declared to be thrown
        KeyStore keyStore = KeyStore.getInstance(CLIENT_KEYSTORE_TYPE);
                                                ^
APIClient.java:40: error: unreported exception NoSuchAlgorithmException; must be caught or declared to be thrown
        keyStore.load(null, clientPassword.toCharArray());
                     ^
2 errors

Let me know what I have missed.

CodePudding user response:

You need to catch or throw also these KeyStoreException, CertificateException, NoSuchAlgorithmException

So either:

public static int GetDetails() throws IOException, KeyStoreException, 
                     CertificateException, NoSuchAlgorithmException {

or

KeyStore keyStore = null;
try {
    keyStore = KeyStore.getInstance(CLIENT_KEYSTORE_TYPE);
    keyStore.load(null, clientPassword.toCharArray());
} catch (KeyStoreException | CertificateException | NoSuchAlgorithmException e) {
    e.printStackTrace();
}
  •  Tags:  
  • java
  • Related