title: "PKIX path building failed" and "unable to find valid certification path to requested target"
body: <p>I'm trying to get tweets using twitter4j library for my java project which uses under the covers <code>java.net.HttpURLConnection</code> (as can be seen in stack trace). On my first run I got an error about certificate <code>sun.security.validator.ValidatorException</code> and <code>sun.security.provider.certpath.SunCertPathBuilderException</code>. Then I added twitter certificate by:</p>
  <pre><code>C:\Program Files\Java\jdk1.7.0_45\jre\lib\security&gt;keytool -importcert -trustcacerts -file PathToCert -alias ca_alias -keystore &quot;C:\Program Files\Java\jdk1.7.0_45\jre\lib\security\cacerts&quot;
  </code></pre>
  <p>But without success. Here is the procedure to get tweets:</p>
  <pre><code>public static void main(String[] args) throws TwitterException {
      ConfigurationBuilder cb = new ConfigurationBuilder();
      cb.setDebugEnabled(true)
          .setOAuthConsumerKey(&quot;myConsumerKey&quot;)
          .setOAuthConsumerSecret(&quot;myConsumerSecret&quot;)
          .setOAuthAccessToken(&quot;myAccessToken&quot;)
          .setOAuthAccessTokenSecret(&quot;myAccessTokenSecret&quot;);
      
      TwitterFactory tf = new TwitterFactory(cb.build());
      Twitter twitter = tf.getInstance();
      
      try {
          Query query = new Query(&quot;iphone&quot;);
          QueryResult result;
          result = twitter.search(query);
          System.out.println(&quot;Total amount of tweets: &quot; + result.getTweets().size());
          List&lt;Status&gt; tweets = result.getTweets();
          
          for (Status tweet : tweets) {
              System.out.println(&quot;@&quot; + tweet.getUser().getScreenName() + &quot; : &quot; + tweet.getText());
          }
      } catch (TwitterException te) {
          te.printStackTrace();
          System.out.println(&quot;Failed to search tweets: &quot; + te.getMessage());
      }
  </code></pre>
  <p>And here is the error:</p>
  <pre><code>sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  Relevant discussions can be found on the Internet at:
      http://www.google.co.jp/search?q=d35baff5 or
      http://www.google.co.jp/search?q=1446302e
  TwitterException{exceptionCode=[d35baff5-1446302e 43208640-747fd158 43208640-747fd158 43208640-747fd158], statusCode=-1, message=null, code=-1, retryAfter=-1, rateLimitStatus=null, version=3.0.5}
      at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:177)
      at twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:61)
      at twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:81)
      at twitter4j.TwitterImpl.get(TwitterImpl.java:1929)
      at twitter4j.TwitterImpl.search(TwitterImpl.java:306)
      at jku.cc.servlets.TweetsAnalyzer.main(TweetsAnalyzer.java:38)
  Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.ssl.Alerts.getSSLException(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
      at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
      at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
      at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
      at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
      at sun.security.ssl.Handshaker.processLoop(Unknown Source)
      at sun.security.ssl.Handshaker.process_record(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
      at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
      at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
      at java.net.HttpURLConnection.getResponseCode(Unknown Source)
      at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(Unknown Source)
      at twitter4j.internal.http.HttpResponseImpl.&lt;init&gt;(HttpResponseImpl.java:34)
      at twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:141)
      ... 5 more
  Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
      at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
      at sun.security.validator.Validator.validate(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
      at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
      ... 20 more
  Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
      at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
      at java.security.cert.CertPathBuilder.build(Unknown Source)
      ... 26 more
  Failed to search tweets: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
  </code></pre>