java - Exception in thread "main" No trusted certificate found



I am in first in automated testing. So, I have a task to create automated script for API testing. I need to create https request with self-signed serificate. This certificate gave me our admins with p12 extension and password file. I exported this certificate to .cer extenssion. Than I created the trust store and import this certificate to this store.

C:\Program Files (x86)\Java\jdk1.8.0_25\jre\bin>keytool -import -keystore clienttrust.jks -file marta.cer -storepass storepass

I located the marta.cer and clienttrust.jks near my project. I have writing my code in IntelliJ IDEA 13.1.1 on Java.

Here is code:

public void testSimpleHttpsClient() throws CertificateException, InterruptedException, UnrecoverableKeyException, NoSuchAlgorithmException,
        IOException, KeyManagementException, KeyStoreException {

    URL url = new URL("https://intstg1-kaakioskpublicapi.ptstaging.ptec/TLE/36171/player/info");

    HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
    con.setRequestMethod( "GET" );

    SSLContext sslContext = SSLContext.getInstance("TLS");

    char[]  passphrase = "storepass".toCharArray();
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(this.getClass().getResourceAsStream("clienttrust.jks"), passphrase);

    TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");


    HostnameVerifier hostnameVerifier = new HostnameVerifier() {

        public boolean verify(String s, SSLSession sslSession) {
            return s.equals(sslSession.getPeerHost());

    sslContext.init(null, tmf.getTrustManagers(), null);

    int responseCode = con.getResponseCode();
    InputStream inputStream;
    if (responseCode == HttpURLConnection.HTTP_OK) {
        inputStream = con.getInputStream();
    } else {
        inputStream = con.getErrorStream();

    // Process the response
    BufferedReader reader;
    String line = null;
    reader = new BufferedReader( new InputStreamReader( inputStream ) );
    while( ( line = reader.readLine() ) != null )
        System.out.println( line );


So, after this code executes I get the next Exception:

Exception in thread "main" No trusted certificate found at at at at at at at at at at at at at at at at at at at httpConnection.Connection.testSimpleHttpsClient( at httpConnection.Connection.main( at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke( at sun.reflect.DelegatingMethodAccessorImpl.invoke( at java.lang.reflect.Method.invoke( at com.intellij.rt.execution.application.AppMain.main( Caused by: No trusted certificate found at at at at at at at ... 20 more

Guys, please explain to me how to solve this problem.

1 Answer: 

Import the server cert as trusted

keytool -import -trustcacerts -alias somealias -keystore clienttrust.jks -file marta.cer