I've been exploring web3j and got the credentials working so I'm able to send transactions from an ethereum address. Now I want to send a transaction to an already existing smart contract and use a function in the smart contract. I've been trying to find a way to do this in the documentation and online but haven't had any luck with it.

How to sent a transaction to an already existing smart contract address and use a function / method in the smart contract?

2 Answers 2


You can use java to interact with your smart contract using web3j.


You can still use solidity and truffle for the smart contract development and use the web3j command:

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

(the .json file is in your build/contracts folder in truffle)

to create a java wrapper of your contract. Using web3j and this generated java file you can interact with the smart contract from java.

check out this post from the web3j creator about using truffle/solidity and java


  • Place all solidity contracts under src/main/solidity directory
  • Add below dependency to build.gradle file
      plugins {
            id 'org.web3j' version '4.8.7'
        solidity {
            executable = './src/main/resources/solc-windows.exe'    // Path to Solc Compiler file
            version = '0.8.0'
        node {
            nodeModulesDir = file("${project.projectDir}/src/main/node")
            version  = '12.16.1'
            download = false
            distBaseUrl = 'http://nodejs.org/dist'
            npmWorkDir = file("${project.projectDir}/src/main/node")
            workDir = file("${project.projectDir}/src/main/node")
  • Run task(generateContractWrappers), wrapper contracts will be generated under build/generated/sources/web3j/ERC20.java

  • Generate contract instance using above wrapper

    Credentials credential=Credentials.create(privatekey);
    //contract gas provider can be created from DefaultGasProvider or StaticGasProvider
    ERC20 contractInstance=ERC20.load(String contractAddress, Web3j web3j, Credentials credentials, ContractGasProvider contractGasProvider);
  • Call contract functions
    BigInteger totalSupply=contractInstance.totalSupply().send();
  • Do not copy & paste the same answer to multiple questions. You should flag the questions as duplicates instead.
    – Ismael
    Commented Oct 19, 2022 at 22:42

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.