I wrote this code in TS using web3 library:

// ABI = my deployed SmartContract as json ABI
const daiToken = new web3.eth.Contract(Abi as AbiItem[], smartContractAdress)
const smartContractFunction = daiToken.methods.smartContractFunction(smartContractFunctionArg)
let smartContractFunctionResult = await smartContractFunction.call()

It works, it's great for the job, but I need to write it in JAVA

Somehow I cannot find anywhere the same functionality in Java's org.web3j.core or org.web3j.contract. I searched documentation, code itself, the internet and nothing. Could samone provide some hints of code example of which classes or interfaces should I use?

1 Answer 1


nice to meet you. I basically understand your issue and that will be helpful for you.

The Web3j library in Java provides a similar function set to that of Web3.js in JavaScript for interacting with the Ethereum network.

However, unlike Web3.js where you can directly interact with the contract using the ABI and contract address, in Web3j you often generate a Java class that corresponds to the deployed smart contract. This class is then used to interact with the contract.

First, you need to generate a Java class for your contract. You can use the web3j command-line tools for this:

web3j truffle generate [--javaTypes|--solidityTypes] /path/to/<truffle-smart-contract>.json -o /path/to/src/main/java -p com.your.organisation.name

This will generate a Java class for your contract in the specified package.

Then, in your Java code, you can load the contract and call its methods:

import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.tx.gas.DefaultGasProvider;

// Connect to the Ethereum network
Web3j web3j = Web3j.build(new HttpService("https://your.network.url"));

// Load the contract
MyContract contract = MyContract.load(contractAddress, web3j, credentials, new DefaultGasProvider());

// Call the contract function
TransactionReceipt transactionReceipt = contract.myFunction(myFunctionArg).send();

In this example, MyContract is the Java class generated from the smart contract, and myFunction is the method in the smart contract.

Please note that send() method can throw an exception, so you should handle it appropriately. Also, replace "https://your.network.url" with the URL of your Ethereum network, and supply your own credentials and contract address.

Remember to add the necessary dependencies to your project's build file:


Please replace the version number with the most current version of Web3j available.

This is a simplified example, and the actual code might need to be adjusted based on your specific requirements and the structure of your smart contract.

  • Thank you very much, it helped a lot :D Commented Oct 29, 2023 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.