I have an existing Smart contract address,abi,binary code. I need to access it from a Java application.Is it possible to do this without creating a java wrapper class?

  • This is possible - but can you elaborate a bit why you do not want a wrapper class? – ligi Mar 18 '18 at 12:50

You can do that, see the documentation von web3j here https://docs.web3j.io/transactions.html

In the Transaction section down in the page

1- Creation of a smart contract.

2- Transacting with a smart contract.

3- Querying the state of a smart contract.

By 1 & 2 don't forget to sign the Transaction. here a simple example:

EthGetTransactionCount ethGetTransactionCount = web3j
                .ethGetTransactionCount("account-address", DefaultBlockParameterName.LATEST).send();
BigInteger nonce = ethGetTransactionCount.getTransactionCount();

// Encode the function
final Function function = new Function(
                "method-name example transfer",
                Arrays.<Type>asList(new org.web3j.abi.datatypes.Address("first parameter example fromAddress"),
                new org.web3j.abi.datatypes.Address("example toAddress"),
                new org.web3j.abi.datatypes.generated.Int256("example value")),
String encodedFunction = FunctionEncoder

// Create new Transaction
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, GASPRICE, GASLIMIT, "contract address", BigInteger.ZERO, encodedFunction);

// Sign the Transaction
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
String hexValue = Numeric.toHexString(signedMessage);

// Send the Transaction
 org.web3j.protocol.core.methods.response.EthSendTransaction transactionResponse = web3j

I was exploring this recently for an android application to interact with arbitrary ERC20 token contracts. You can do it using the address and bin of the contract.

My process for doing it was to create the wrapper class for a related contract, find the functions in it that I needed (as well as the "load" functions), then copy those over to a newly created "generic" wrapper class that could work with any arbitrary contract rather than needing to be tied to just one that has a pre-generated wrapper. As long as you've written the functions you'll be using into the generic wrapper, it'll work well. This works best if you're doing something like interacting with ERC20 token contracts since those have standard functions (transfer, totalSupply, etc). Here's a snippet of my code from the wrapper that would work with any ERC20 token given the bin, address, wallet credentials, and a few other things have been retrieved:

public RemoteCall<TransactionReceipt> transfer(String to, BigInteger tokens) {
    Function function = new Function(
            Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(to), 
            new org.web3j.abi.datatypes.generated.Uint256(tokens)), 
    return executeRemoteCallTransaction(function);


public static Web3jWrapper load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, String binary) {
    BINARY = binary;
        return Web3jWrapper(contractAddress, web3j, credentials, gasPrice, gasLimit);

The rest of the heavy lifting is done behind the scenes with web3j, you just have to change the load function so it takes the binary along with the other parameters and you can load it with:

GenericSmartContract contract = GenericSmartContract.load(
    "0x<address>|<ensName>", web3j, credentials, GAS_PRICE, GAS_LIMIT, binary);

and from there call your functions:

TransactionReceipt receipt = contract.transfer("0x1234567890123456",100000).send();

This is somewhat the standard for interacting with a smart contract via web3.js and linking it in your frontend


    if (typeof web3 !== 'undefined') {
       web3 = new Web3(web3.currentProvider);
   } else {
       web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
   var contractAddress = 'youContractAddress';

   web3.eth.defaultAccount = web3.eth.accounts[0];

   var MyContract = web3.eth.contract();

   var MySiteContract = MyContract.at(contractAddress);

  • web3j is java, you example is for javascript – Ismael Mar 3 '18 at 5:19

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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