user323: I am having problems communicating between Java and bitcoind - every JSON RPC library I try has some issues. Can anyone provide a working implementation of even the most basic JSON RPC communication between Java and bitcoind ?

user2075: Since I could not find working code snippet anywhere, here is a complete working example (in Scala):
First I created a helper object:
import java.net.URL
import java.net.HttpURLConnection
import org.apache.commons.io.IOUtils

object CurlJsonData {
  def curl(url:String, jsonEncodedString:String) = {
    val httpcon = new URL(url).openConnection.asInstanceOf[HttpURLConnection]
    httpcon.setDoOutput(true);
    httpcon.setRequestProperty("Content-Type", "application/json");
    httpcon.setRequestProperty("Accept", "application/json");
    httpcon.setRequestMethod("POST");
    httpcon.connect;

    val outputBytes = jsonEncodedString.getBytes("UTF-8");

    // 'using' method from: https://stackoverflow.com/a/5218279/243233

    using(httpcon.getOutputStream){os =>
      os.write(outputBytes)
    }
    val code = httpcon.getResponseCode
    val isError = code >= 400 && code <= 500
    val resp = using{
      if (isError) httpcon.getErrorStream else httpcon.getInputStream
    }{is =>
      val writer = new StringWriter;
      IOUtils.copy(is, writer, "UTF-8");
      writer.toString;
    }
    httpcon.disconnect
    if (isError) throw new Exception(
      s"Resp code $code. Error: ${resp.take(200)}"
    ) else resp
  }
}

Then, I used it as follows:
import java.net.Authenticator
import java.net.PasswordAuthentication

val rpcuser = "alice";
val rpcpassword = "secret";

Authenticator.setDefault(
  new Authenticator {
    override def getPasswordAuthentication:PasswordAuthentication = {
      new PasswordAuthentication (rpcuser, rpcpassword.toCharArray)
    }
  }
)  

CurlJsonData.curl(
  "http://localhost:8332", 
  """{"method":"getblockchaininfo","params":[],"id":1,"jsonrpc":"1.0"}"""
) 


user91194: One of the problems of Java is the verbosity of the language. However, it is also true that the life of the language it is very old in these that.
I fall into the problem to talk with the bitcoin rpc interface for one of my side projects on c-lightning and I was not able to find a clean library that is able to talk also with other rpc interfaces derived from bitcoin core, like litecoin. In addition, I found also a library that is easy like a python library.
My result of designing a library that respect this requirement, it is called lite-bitcoin-rpc and it is available on GitHub https://github.com/clightning4j/lite-bitcoin-rpc
This is an easy library because with gives the possibility to write the JSON wrapper in a Java class and use it to decode the response. The end-user need only to feel a map with parameters and create the Java class where decode the JSON payload received from bitcoin core.
An example can be found in the test directory https://github.com/clightning4j/lite-bitcoin-rpc/tree/main/lib/src/test
and a code example can be
public class LiteBitcoinRPCTest {

  private LiteBitcoinRPC bitcoinRPC;

  public LiteBitcoinRPCTest() {
    this.bitcoinRPC = new LiteBitcoinRPC("sandbox", "sandbox", "http://127.0.0.1:18333/");
  }

  @Test
  public void getBlockchainInfo() {
    try {
      BlockchainInfo info =
          bitcoinRPC.makeBitcoinRequest("getblockchaininfo", BlockchainInfo.class);
      TestCase.assertEquals(info.getChain(), "regtest");
    } catch (Exception e) {
      e.printStackTrace();
      TestCase.fail(e.getLocalizedMessage());
    }
  }

  @Test
  public void estimateFeeRateWithError() {
    Parameters parameters = new Parameters("estimatesmartfee");
    parameters.addParameter("conf_target", 6);
    try {
      BitcoinEstimateFee feee = bitcoinRPC.makeBitcoinRequest(parameters, BitcoinEstimateFee.class);
      TestCase.assertFalse(feee.getErrors().isEmpty());
    } catch (LiteBitcoinRPCException | BitcoinCoreException e) {
      TestCase.fail(e.getMessage());
    }
  }


user13642: https://github.com/nitinsurana/Litecoin-Bitcoin-RPC-Java-Connector
It uses Htmlunit instead of Apache Http Library, which makes it a bit easy to understand and extend.
I actually wrote & tested it for Litecoin for one of my projects. But it has been extended to support bitcoin and all the RPC methods are available.

user32542: Library under Apache Licence:
https://github.com/SulacoSoft/BitcoindConnector4J

user6643: I had the same issue and created an implementation here: https://github.com/johannbarbie/BitcoindClient4J

user3576: Because this seems like a collection of links, I will just add another one:
https://github.com/priiduneemre/btcd-cli4j

user4524: You are also very welcome to try out https://github.com/clanie/bitcoind-client - it is in early development, but already supports almost all the methods provided by bitcoind.

user1013: Here's an early experimental client I had played around with a while back. It supports getInfo, getBalance and getNewAddress, and can easily be expanded. In order to run it, the credentials for your local bitcoind have to match the values in the client class:
httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8332),
                    new UsernamePasswordCredentials("btc", "123"));

Feel free to use this code in any way, but understand that it's only 5 minutes of work on a start, not a realistic client implementation. Hope it helps you generate some ideas!

323: <p>I am having problems communicating between Java and bitcoind - every JSON RPC library I try has some issues. Can anyone provide a working implementation of even the most basic JSON RPC communication between Java and bitcoind ?</p>
 2075: <p>Since I could not find working code snippet anywhere, here is a complete working example (in Scala):</p>

<p>First I created a helper object:</p>

<pre><code>import java.net.URL
import java.net.HttpURLConnection
import org.apache.commons.io.IOUtils

object CurlJsonData {
  def curl(url:String, jsonEncodedString:String) = {
    val httpcon = new URL(url).openConnection.asInstanceOf[HttpURLConnection]
    httpcon.setDoOutput(true);
    httpcon.setRequestProperty("Content-Type", "application/json");
    httpcon.setRequestProperty("Accept", "application/json");
    httpcon.setRequestMethod("POST");
    httpcon.connect;

    val outputBytes = jsonEncodedString.getBytes("UTF-8");

    // 'using' method from: https://stackoverflow.com/a/5218279/243233

    using(httpcon.getOutputStream){os =&gt;
      os.write(outputBytes)
    }
    val code = httpcon.getResponseCode
    val isError = code &gt;= 400 &amp;&amp; code &lt;= 500
    val resp = using{
      if (isError) httpcon.getErrorStream else httpcon.getInputStream
    }{is =&gt;
      val writer = new StringWriter;
      IOUtils.copy(is, writer, "UTF-8");
      writer.toString;
    }
    httpcon.disconnect
    if (isError) throw new Exception(
      s"Resp code $code. Error: ${resp.take(200)}"
    ) else resp
  }
}
</code></pre>

<p>Then, I used it as follows:</p>

<pre><code>import java.net.Authenticator
import java.net.PasswordAuthentication

val rpcuser = "alice";
val rpcpassword = "secret";

Authenticator.setDefault(
  new Authenticator {
    override def getPasswordAuthentication:PasswordAuthentication = {
      new PasswordAuthentication (rpcuser, rpcpassword.toCharArray)
    }
  }
)  

CurlJsonData.curl(
  "http://localhost:8332", 
  """{"method":"getblockchaininfo","params":[],"id":1,"jsonrpc":"1.0"}"""
) 
</code></pre>
 91194: <p>One of the problems of Java is the verbosity of the language. However, it is also true that the life of the language it is very old in these that.</p>
<p>I fall into the problem to talk with the bitcoin rpc interface for one of my side projects on c-lightning and I was not able to find a clean library that is able to talk also with other rpc interfaces derived from bitcoin core, like litecoin. In addition, I found also a library that is easy like a python library.</p>
<p>My result of designing a library that respect this requirement, it is called lite-bitcoin-rpc and it is available on GitHub <a href="https://github.com/clightning4j/lite-bitcoin-rpc" rel="nofollow noreferrer">https://github.com/clightning4j/lite-bitcoin-rpc</a></p>
<p>This is an easy library because with gives the possibility to write the JSON wrapper in a Java class and use it to decode the response. The end-user need only to feel a map with parameters and create the Java class where decode the JSON payload received from bitcoin core.</p>
<p>An example can be found in the test directory <a href="https://github.com/clightning4j/lite-bitcoin-rpc/tree/main/lib/src/test" rel="nofollow noreferrer">https://github.com/clightning4j/lite-bitcoin-rpc/tree/main/lib/src/test</a></p>
<p>and a code example can be</p>
<pre><code>public class LiteBitcoinRPCTest {

  private LiteBitcoinRPC bitcoinRPC;

  public LiteBitcoinRPCTest() {
    this.bitcoinRPC = new LiteBitcoinRPC(&quot;sandbox&quot;, &quot;sandbox&quot;, &quot;http://127.0.0.1:18333/&quot;);
  }

  @Test
  public void getBlockchainInfo() {
    try {
      BlockchainInfo info =
          bitcoinRPC.makeBitcoinRequest(&quot;getblockchaininfo&quot;, BlockchainInfo.class);
      TestCase.assertEquals(info.getChain(), &quot;regtest&quot;);
    } catch (Exception e) {
      e.printStackTrace();
      TestCase.fail(e.getLocalizedMessage());
    }
  }

  @Test
  public void estimateFeeRateWithError() {
    Parameters parameters = new Parameters(&quot;estimatesmartfee&quot;);
    parameters.addParameter(&quot;conf_target&quot;, 6);
    try {
      BitcoinEstimateFee feee = bitcoinRPC.makeBitcoinRequest(parameters, BitcoinEstimateFee.class);
      TestCase.assertFalse(feee.getErrors().isEmpty());
    } catch (LiteBitcoinRPCException | BitcoinCoreException e) {
      TestCase.fail(e.getMessage());
    }
  }
</code></pre>
 13642: <p><a href="https://github.com/nitinsurana/Litecoin-Bitcoin-RPC-Java-Connector" rel="nofollow noreferrer">https://github.com/nitinsurana/Litecoin-Bitcoin-RPC-Java-Connector</a></p>

<p>It uses Htmlunit instead of Apache Http Library, which makes it a bit easy to understand and extend.</p>

<p>I actually wrote &amp; tested it for Litecoin for one of my projects. But it has been extended to support bitcoin and all the RPC methods are available.</p>
 32542: <p>Library under Apache Licence:
<a href="https://github.com/SulacoSoft/BitcoindConnector4J" rel="nofollow">https://github.com/SulacoSoft/BitcoindConnector4J</a></p>
 6643: <p>I had the same issue and created an implementation here: <a href="https://github.com/johannbarbie/BitcoindClient4J" rel="nofollow">https://github.com/johannbarbie/BitcoindClient4J</a></p>
 3576: <p>Because this seems like a collection of links, I will just add another one:</p>

<p><a href="https://github.com/priiduneemre/btcd-cli4j" rel="nofollow">https://github.com/priiduneemre/btcd-cli4j</a></p>
 4524: <p>You are also very welcome to try out <a href="https://github.com/clanie/bitcoind-client" rel="nofollow">https://github.com/clanie/bitcoind-client</a> - it is in early development, but already supports almost all the methods provided by bitcoind.</p>
 1013: <p><a href="https://github.com/cdelargy/BitcoinRPCClient/blob/master/src/main/java/com/cdelargy/bitcoin/RPCClient.java">Here's an early experimental client I had played around with a while back</a>. It supports getInfo, getBalance and getNewAddress, and can easily be expanded. In order to run it, the credentials for your local bitcoind have to match the values in the client class:</p>

<pre><code>httpclient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8332),
                    new UsernamePasswordCredentials("btc", "123"));
</code></pre>

<p>Feel free to use this code in any way, but understand that it's only 5 minutes of work on a start, not a realistic client implementation. Hope it helps you generate some ideas!</p>
