title: How to get jenkins job build status using java
body: <p>I'm trying to write a java program which will provide the build status of a particular job, this Jenkins job is running at every hour and I want to get the build status(success or failed) of each build for the last 24 hours. I can get the build information from the below url format : /job//api/json?tree=allBuilds[result,number,url,timestamp] OR
  <a href="http://jenkins.myteam-aws.local:8080/job/HealthCheckerApp/api/json?tree=allBuilds[result,number,url,timestamp]" rel="nofollow noreferrer">http://jenkins.myteam-aws.local:8080/job/HealthCheckerApp/api/json?tree=allBuilds[result,number,url,timestamp]</a>
  But the problem is when I logged in to the application I can retrieve the information but when I call the above url from the java application then I am getting the 500 error, possible cause of this error could be login credentials are missing.
  So how could I read this json data from the URL?
  Seeking your help in this regard.</p>
  
  <pre><code>// below code will read the json data from the given url
  
  public class JsonReader {
  
  private static String readAll(Reader rd) throws IOException {
      StringBuilder sb = new StringBuilder();
      int cp;
      while ((cp = rd.read()) != -1) {
          sb.append((char) cp);
      }
      return sb.toString();
  }
  
  public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
      InputStream is = new URL(url).openStream();
      try {
          BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
          String jsonText = readAll(rd);
          JSONObject json = new JSONObject(jsonText);
          return json;
      } finally {
          is.close();
      }
  }
  
  public static void main(String[] args) throws IOException, JSONException {
      System.out.println("inside main--------&gt;");
      JSONObject json = readJsonFromUrl("http://jenkins.myteam-aws.local:8080/job/HealthCheckerApp/api/json?tree=allBuilds[result,number,url,timestamp]");
  
      System.out.println("Json object created: " + json);
      System.out.println(json.toString());
  }
  }
  </code></pre>
  
  <p>Result: SUCCESS</p>
  
  <p>Built No: 4715</p>
  
  <p>url: <a href="http://jenkins.myteam-aws.local:8080/job/Thor-API-Prod-Health-Checker/4715/" rel="nofollow noreferrer">http://jenkins.myteam-aws.local:8080/job/Thor-API-Prod-Health-Checker/4715/</a></p>
  
  <p>Timestamp: 1545803471449</p>