Splunk Dev

How do I connect my Java program to Splunkd server?

madhanbaskar
Explorer

I'm using the below mentioned code in java:

import com.splunk.*; // The entry point to the client library
public class Connection
{
public static void main(String[] args)
{
ServiceArgs loginArgs = new ServiceArgs();
loginArgs.setUsername("admin");
loginArgs.setPassword("changeme");
loginArgs.setHost("apl10865gtapp14");
loginArgs.setPort(8089);
loginArgs.setScheme("http");
HttpService.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_2);
Service service = Service.connect(loginArgs);
for (Application app : service.getApplications().values())
{
System.out.println(app.getName());
}}}

Below mentioned is the error which I get....

Exception in thread "main" java.lang.RuntimeException: Connection refused: connect
at com.splunk.HttpService.send(HttpService.java:409)
at com.splunk.Service.send(Service.java:1293)
at com.splunk.HttpService.post(HttpService.java:308)
at com.splunk.Service.login(Service.java:1122)
at com.splunk.Service.login(Service.java:1101)
at com.splunk.Service.connect(Service.java:187)
at Connection.main(Connection.java:13)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at sun.net.NetworkClient.doConnect(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.openServer(Unknown Source)
at sun.net.www.http.HttpClient.(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.http.HttpClient.New(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream0(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at com.splunk.HttpService.send(HttpService.java:403)
... 6 more

Not sure what the problem is..!!

Can you guys please help me out on this?

0 Karma

s2_splunk
Splunk Employee
Splunk Employee

"Caused by: java.net.ConnectException: Connection refused: connect"

Have you validated (using telnet, maybe) that you have network connectivity between your source host and your target host apl10865gtapp14?

0 Karma

peter_krammer
Communicator

You probably don't have Firewall Permissions from were you run your code to your splunk instance on port 8089?

0 Karma

nickhills
Ultra Champion

Whilst I wont be any help with solving any Java issues, (I'm not a java dev) the management port on 8089 is https, but it looks like you are using an http scheme to connect.
Try using https: loginArgs.setScheme("https");

If my comment helps, please give it a thumbs up!
0 Karma

madhanbaskar
Explorer

@nickhillscpl

I have changed it to https. Still the same..:(

0 Karma

madhanbaskar
Explorer

@Damien Dallimore

Could you please help?

0 Karma

Damien_Dallimor
Ultra Champion

Because of : Caused by: java.net.ConnectException: Connection refused: connect

I am guessing firewall.

0 Karma
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Agent Mode Engaged! Enchaining Agentic Operations with Splunk AI Assistant 2.0

    Are you ready to transform how your team handles complex data requests? We invite you to our upcoming ...

Announcing Modern Navigation: A New Era of Splunk User Experience

We are excited to introduce the Modern Navigation feature in the Splunk Platform, available to both cloud and ...

Modernize your Splunk Apps – Introducing Python 3.13 in Splunk

We are excited to announce that the upcoming releases of Splunk Enterprise 10.2.x and Splunk Cloud Platform ...