First write a smart contract in solidity.
pragma solidity ^0.4.17;
contract TestMethod{
string public name = "Reverb";
function updateName(string _name) public{
name = _name;
}
}
This compile it and generate wrapper classes with the help of below commands. Use solidity compiler first and execute command it will give you one bin and one abi file.
>> solc TestMethod.sol --bin --abi --optimize -o ./
Use web3j wrapper file generator and with the help of abi and bin file, execute the below command.
>> web3j solidity generate TestMethod.bin TestMethod.abi -o . -p org.web3j.samp
le
You will get a java wrapper class like this.
package org.web3j.sample;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;
import org.web3j.tx.TransactionManager;
/**
* <p>Auto generated code.
* <p><strong>Do not modify!</strong>
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
*
* <p>Generated with web3j version 3.4.0.
*/
public class TestMethod extends Contract {
private static final String BINARY = "60c0604052600660808190527f526576657262000000000000000000000000000000000000000000000000000060a090815261003e9160009190610051565b5034801561004b57600080fd5b506100ec565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009257805160ff19168380011785556100bf565b828001600101855582156100bf579182015b828111156100bf5782518255916020019190600101906100a4565b506100cb9291506100cf565b5090565b6100e991905b808211156100cb57600081556001016100d5565b90565b6102a1806100fb6000396000f30060806040526004361061004b5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166306fdde03811461005057806384da92a7146100da575b600080fd5b34801561005c57600080fd5b50610065610135565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561009f578181015183820152602001610087565b50505050905090810190601f1680156100cc5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156100e657600080fd5b506040805160206004803580820135601f81018490048402850184019095528484526101339436949293602493928401919081908401838280828437509497506101c39650505050505050565b005b6000805460408051602060026001851615610100026000190190941693909304601f810184900484028201840190925281815292918301828280156101bb5780601f10610190576101008083540402835291602001916101bb565b820191906000526020600020905b81548152906001019060200180831161019e57829003601f168201915b505050505081565b80516101d69060009060208401906101da565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061021b57805160ff1916838001178555610248565b82800160010185558215610248579182015b8281111561024857825182559160200191906001019061022d565b50610254929150610258565b5090565b61027291905b80821115610254576000815560010161025e565b905600a165627a7a723058202be32a6af265c5583cba0e7c6731a4b971eeac0ec50f7b8aaf89a9e21d33184d0029";
public static final String FUNC_NAME = "name";
public static final String FUNC_UPDATENAME = "updateName";
protected TestMethod(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}
protected TestMethod(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
public RemoteCall<String> name() {
final Function function = new Function(FUNC_NAME,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}
public RemoteCall<TransactionReceipt> updateName(String _name) {
final Function function = new Function(
FUNC_UPDATENAME,
Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_name)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
public static RemoteCall<TestMethod> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(TestMethod.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}
public static RemoteCall<TestMethod> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(TestMethod.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
}
public static TestMethod load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new TestMethod(contractAddress, web3j, credentials, gasPrice, gasLimit);
}
public static TestMethod load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new TestMethod(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
}
Further you can deploy the contract with the deploy() method and use
updateName() method for updating the data. For more information,
please refer to this blog.
https://medium.com/coinmonks/ethereum-blockchain-hello-world-smart-contract-with-java-9b6ae2961ad1