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
Get Updates on the Splunk Community!

Splunk MCP & Agentic AI: Machine Data Without Limits

  Discover how the Splunk Model Context Protocol (MCP) Server can revolutionize the way your organization ...

Finding Based Detections General Availability

Overview  We’ve come a long way, folks, but here in Enterprise Security 8.4 I’m happy to announce Finding ...

Get Your Hands Dirty (and Your Shoes Comfy): The Splunk Experience

Hands-On Learning and Technical Seminars  Sometimes, you just need to see the code. For those looking for a ...