<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: fail to connect with java sdk in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/fail-to-connect-with-java-sdk/m-p/96797#M1374</link>
    <description>&lt;P&gt;Hi , &lt;BR /&gt;
Here I will resolve that issue ,&lt;BR /&gt;
Follow that step &lt;/P&gt;

&lt;P&gt;public class SplunkConnection {&lt;/P&gt;

&lt;P&gt;public String  secureConnection() throws JSONException, IOException{&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; HttpService.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_2);

    Map &amp;lt;String,Object&amp;gt; connectionArgs = new HashMap&amp;lt;String,Object&amp;gt;();
    connectionArgs.put("host", "DEVELOPMENT");
    connectionArgs.put("username", "admin");
    connectionArgs.put("password", "*****");
    connectionArgs.put("port", 8089);
    connectionArgs.put("scheme", "https");
    Service splunkService = Service.connect(connectionArgs);
    Args queryArgs = new Args();
    queryArgs.put("earliest_time","0");
    queryArgs.put("latest_time","now");
Job job = splunkService.getJobs().create("search index=*");
while(!job.isDone()){
    try{
        Thread.sleep(500);
    }catch (Exception exp){
        exp.printStackTrace();
    }
}
try{
    Args outputArgs = new Args();
    outputArgs.put("output_mode", "json");
    InputStream inputStream = job.getResults(outputArgs);
    byte[] buffer = new byte[4096];
    String json= "";
    while(inputStream.read(buffer)!= -1){
        json = new String(buffer);
        logger.info(json );     
    }
    return json;

} catch(Exception e){
    e.printStackTrace();
}
    return null;
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;}&lt;BR /&gt;
Thank you,&lt;BR /&gt;
If you get any issue please let me know.&lt;/P&gt;</description>
    <pubDate>Tue, 16 Feb 2016 07:44:54 GMT</pubDate>
    <dc:creator>erritesh17</dc:creator>
    <dc:date>2016-02-16T07:44:54Z</dc:date>
    <item>
      <title>fail to connect with java sdk</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/fail-to-connect-with-java-sdk/m-p/96795#M1372</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I'm trying to connect to Splunk API through java sdk, but get stuck when creating a job.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;// Create a map of arguments and add login parameters
    ServiceArgs loginArgs = new ServiceArgs();
    loginArgs.setHost("localhost");
    loginArgs.setPort(8089);

    // Connect to Splunk
    Service service = Service.connect(loginArgs);

    JobArgs jobargs = new JobArgs();
    jobargs.setExecutionMode(JobArgs.ExecutionMode.NORMAL);
    JobCollection jobs = service.getJobs();
    Job job = jobs.create("search * | head 5", jobargs); // &amp;lt;&amp;lt;--- this fails

    // Wait for the search to finish
    while (!job.isDone()) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    // Get the search results 
    try (InputStream resultsNormalSearch = job.getResults()) {
        String inputStreamString = new Scanner(resultsNormalSearch, "UTF-8").useDelimiter("\\A").next();
        System.out.println(inputStreamString);
    } catch (IOException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I'm running Splunk Java SDK 1.2, Splunk Free licence 6.0, Java 7, Windows 7&lt;/P&gt;

&lt;P&gt;It works in Splunk 5.0.4 but running against Splunk 6 I get the following error:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[Fatal Error] :1:3: Dokumentets kodtext före rotelementet måste vara välformulerad. --&amp;gt; in english --&amp;gt; Document code text before the root element must be well-formed.

Exception in thread "main" com.splunk.HttpException: HTTP 400
    at com.splunk.HttpException.create(HttpException.java:59)
    at com.splunk.HttpService.send(HttpService.java:355)
    at com.splunk.Service.send(Service.java:1203)
    at com.splunk.HttpService.post(HttpService.java:212)
    at com.splunk.JobCollection.create(JobCollection.java:79)
    at com.splunk.JobCollection.create(JobCollection.java:111)
    at se.lul.fris.splunkpinger.Main2.main(Main2.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Does anyone know what's wrong? &lt;BR /&gt;
Thanks&lt;/P&gt;</description>
      <pubDate>Mon, 14 Oct 2013 14:32:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/fail-to-connect-with-java-sdk/m-p/96795#M1372</guid>
      <dc:creator>perseger</dc:creator>
      <dc:date>2013-10-14T14:32:09Z</dc:date>
    </item>
    <item>
      <title>Re: fail to connect with java sdk</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/fail-to-connect-with-java-sdk/m-p/96796#M1373</link>
      <description>&lt;P&gt;Did you end up fixing this issue? If so, how? I am facing the same issue now.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2015 18:20:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/fail-to-connect-with-java-sdk/m-p/96796#M1373</guid>
      <dc:creator>bgadsk</dc:creator>
      <dc:date>2015-09-29T18:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: fail to connect with java sdk</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/fail-to-connect-with-java-sdk/m-p/96797#M1374</link>
      <description>&lt;P&gt;Hi , &lt;BR /&gt;
Here I will resolve that issue ,&lt;BR /&gt;
Follow that step &lt;/P&gt;

&lt;P&gt;public class SplunkConnection {&lt;/P&gt;

&lt;P&gt;public String  secureConnection() throws JSONException, IOException{&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; HttpService.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_2);

    Map &amp;lt;String,Object&amp;gt; connectionArgs = new HashMap&amp;lt;String,Object&amp;gt;();
    connectionArgs.put("host", "DEVELOPMENT");
    connectionArgs.put("username", "admin");
    connectionArgs.put("password", "*****");
    connectionArgs.put("port", 8089);
    connectionArgs.put("scheme", "https");
    Service splunkService = Service.connect(connectionArgs);
    Args queryArgs = new Args();
    queryArgs.put("earliest_time","0");
    queryArgs.put("latest_time","now");
Job job = splunkService.getJobs().create("search index=*");
while(!job.isDone()){
    try{
        Thread.sleep(500);
    }catch (Exception exp){
        exp.printStackTrace();
    }
}
try{
    Args outputArgs = new Args();
    outputArgs.put("output_mode", "json");
    InputStream inputStream = job.getResults(outputArgs);
    byte[] buffer = new byte[4096];
    String json= "";
    while(inputStream.read(buffer)!= -1){
        json = new String(buffer);
        logger.info(json );     
    }
    return json;

} catch(Exception e){
    e.printStackTrace();
}
    return null;
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;}&lt;BR /&gt;
Thank you,&lt;BR /&gt;
If you get any issue please let me know.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Feb 2016 07:44:54 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/fail-to-connect-with-java-sdk/m-p/96797#M1374</guid>
      <dc:creator>erritesh17</dc:creator>
      <dc:date>2016-02-16T07:44:54Z</dc:date>
    </item>
    <item>
      <title>Re: fail to connect with java sdk</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/fail-to-connect-with-java-sdk/m-p/96798#M1375</link>
      <description>&lt;P&gt;Still Getting same error.. Can you resolve this&lt;/P&gt;</description>
      <pubDate>Wed, 14 Mar 2018 14:07:19 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/fail-to-connect-with-java-sdk/m-p/96798#M1375</guid>
      <dc:creator>npbala_22</dc:creator>
      <dc:date>2018-03-14T14:07:19Z</dc:date>
    </item>
  </channel>
</rss>

