<dependency>
<groupId>org.web3j</groupId>
<artifactId>core</artifactId>
<version>X.X.X</version>
</dependency>
Web3j web3 = Web3j.build(new HttpService("http://localhost:8545"));
solidity
pragma solidity ^0.8.0;
contract MyContract {
uint public myVariable;
function set(uint newValue) public {
myVariable = newValue;
}
}
bash
import org.web3j.abi.datatypes.Uint;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.DefaultBlockParameter;
import org.web3j.protocol.core.methods.request.Transaction;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;
import org.web3j.tx.ManagedTransaction;
import java.math.BigInteger;
public class Main {
public static void main(String[] args) throws Exception {
Web3j web3 = Web3j.build(new HttpService("http://localhost:8545"));
MyContract contract = MyContract.deploy(
).send();
Transaction transaction = Transaction.createEtherTransaction(
credentials.getAddress(),
contract.getContractAddress(),
BigInteger.valueOf(0),
BigInteger.valueOf(21_000),
contract.myFunction(new Uint(42)).encodeABI()
);
web3.ethSendTransaction(transaction).send();
}
}