这里需要使用的环境 web3j,nodejs

  • 安装编译sol工具
$ npm install -g solc
  • 保存为hello.sol文件到本地
pragma solidity 0.4.19;
contract hello {
function main(uint a) constant returns (uint b)
{
uint result = a * 8;
return result;
}
}
  • 编译sol文件
$ solcjs <sol文件目录>   --optimize  --bin --abi --output-dir <输出目录>

//demo
$ solcjs F:\\hello.sol --optimize --bin --abi --output-dir F:\\

出现这种错误

Failed to write F:\F_\hello_sol_hello.bin: Error: ENOENT: no such file or directory, open 'F:\F_\hello_sol_hello.bin'
Failed to write F:\F_\hello_sol_hello.abi: Error: ENOENT: no such file or directory, open 'F:\F_\hello_sol_hello.abi'

这里有个坑,就是使用solcjs 编译智能合约文件输出到目录会有一个文件夹,这个需要手动创建,我这里输出目录到F:\\ 但是它还是要输出到F:\\F_\ 下,这里的F_文件夹需要我们创建!

  • 编译成功,记录下bin 和 abi后缀的文件
  • 使用web3j工具生成java版本的智能合约
  • web3j 命令行工具下载
$ web3j solidity generate <编译的bin文件地址> <编译的abi文件地址> -o <输出目录> -p <java包名>
//demo
$ web3j solidity generate F:\F_\hello_sol_hello.bin F:\F_\hello_sol_hello.abi -o E:\etheth\src\main\java -p xyz.lihang.demo.eth.sol

 PS:使用web3j命令,需要进入https://github.com/web3j/web3j/releases/tag/v3.3.1网站,下载web3j-3.3.1.tar,并解压。

   进入目录bin下,在此目录命令行执行web3j,否则web3j bash命令不存在

  • 生成成功
package xyz.lihang.demo.eth.sol;

import java.math.BigInteger;
import java.util.Arrays;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.RemoteCall;
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.2.0.
*/
public class Hello_sol_hello extends Contract {
private static final String BINARY = "60606040523415600e57600080fd5b609a8061001c6000396000f300606060405260043610603e5763ffffffff7c0100000000000000000000000000000000000000000000000000000000600035041663ab3ae25581146043575b600080fd5b3415604d57600080fd5b60566004356068565b60405190815260200160405180910390f35b600802905600a165627a7a723058200cc51f5dad45190b24189d9f8ff836d704bcebc9862cfd669e054b8c8f19f66c0029"; protected Hello_sol_hello(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
} protected Hello_sol_hello(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
} public RemoteCall<BigInteger> main(BigInteger a) {
Function function = new Function("main",
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(a)),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
} public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(Hello_sol_hello.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
} public static RemoteCall<Hello_sol_hello> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(Hello_sol_hello.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
} public static Hello_sol_hello load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new Hello_sol_hello(contractAddress, web3j, credentials, gasPrice, gasLimit);
} public static Hello_sol_hello load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new Hello_sol_hello(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
}

一定要注意合约调用后进行挖矿,这样才能网络确认,才能使用合约

使用web3j工具生成java版本的智能合约的更多相关文章

  1. 使用linux perf工具生成java程序火焰图

    pre.cjk { font-family: "Nimbus Mono L", monospace } p { margin-bottom: 0.1in; line-height: ...

  2. Hyperledger Fabric 2.x 自定义智能合约

    一.说明 为了持续地进行信息的更新,以及对账本进行管理(写入交易,进行查询等),区块链网络引入了智能合约来实现对账本的访问和控制:智能合约在 Fabric 中称之为 链码,是区块链应用的业务逻辑. 本 ...

  3. 使用Truffle 部署智能合约

    使用Truffle 部署智能合约 之前我们使用Geth,原生的以太坊Golang工具,分析了创世区块的参数内容,在本地创建了私有以太坊区块链,并使用两个账户进行了挖矿和转账操作,对以太坊有了基本了解. ...

  4. 使用java的wsimport.exe工具生成wsdl的客户端代码

    在jdk的bin目录下有一个wsimport.exe的工具,使用该工具可以根据wsdl地址生成java的客户端代码. 常用命令如下: wsimport  -keep -d d:\ -s d:\src  ...

  5. 在线数据库表(sql语句)生成java实体类工具

    相信每个做java开发的读者,都接触过SQL建表语句,尤其是在项目开发初期,因为数据库是项目的基石. 在现代项目开发中,出现了许多ORM框架,通过简单的实体映射,即可实现与数据库的交互,然而我们最初设 ...

  6. 微博短链接的生成算法(Java版本)

    最近看到微博的短链接真是很火啊,新浪.腾讯.搜狐等微博网站都加入了短链接的功能.之所以要是使用短链接,主要是因为微博只允许发140 字,如果链接地址太长的话,那么发送的字数将大大减少.短链接的主要职责 ...

  7. java 调用区块链 发布和调用智能合约

    java连接区块链 很简单 ,调用智能合约要麻烦一些. 先说连接 区块链查询数据. 1 maven 项目导入 web3j 的依赖. <dependency> <groupId> ...

  8. 存在即合理,重复轮子orm java版本

    1,业务描述前序? 需求来源于,公司的运营部门.本人所在公司(私营,游戏行业公司),从初创业,我就进入公司,一直致力于服务器核心研发. 公司成立块3年了,前后出产了4款游戏,一直在重复的制造公司游戏对 ...

  9. JGibbLDA:java版本的LDA(Latent Dirichlet Allocation)实现、修改及使用

    转载自:http://blog.csdn.net/memray/article/details/16810763   一.概述 JGibbLDA是一个java版本的LDA(Latent Dirichl ...

随机推荐

  1. Windows下MySQL的绿化与精简

    MySQL本身就支持安装使用,本文只是对自己使用免安装版MySQL的经历记录下来,以便以后查看. 首先是获取Windows下的MySQL免安装版本,这个需要去到MySQL官网进行下载.我一般喜欢把首页 ...

  2. HashMap TreeMap ConcurrentHashMap

    1 HashMap java se 1.6 1.1 父类 java.lang.Object 继承者 java.util.AbstractMap<K,V> 继承者 java.util.Has ...

  3. Java基础语法<八> 继承 多态 抽象 反射

    1.超类和子类 超类和子类 父类与子类 多态: 一个对象变量可以指示多种实际类型的现象称为多态 一个变量可以引用父类对象,也可以引用其子类对象,这就是多态. 不能将一个超类的引用赋给子类变量,因为调用 ...

  4. Linux上强制踢出其他正在登录的用户

    一.查看当前在线用户有几个 w命令 [root@pa1 nginx]#w 13:36:00 up 79 days, 23:50, 3 users, load average: 0.10, 0.07, ...

  5. Django----djagorestframwork使用

    restful(表者征状态转移,面向资源编程)------------------------------------------->约定 从资源的角度审视整个网络,将分布在网络中某个节点的资源 ...

  6. easyui---修改删除查询

    修改:在toolsbar 修改工具中 { text:"编辑用户", iconCls:"icon-edit", handler:function(){ var s ...

  7. 1.7Oob 成员变量可以不初始化 但局部变量必须初始化

    成员变量有默认的初始值 像int a; a默认为0:而局部变量没有初始值

  8. 原来的ALL IN ONE架构,快速的演进成为SOA架构

    原来的ALL IN ONE架构,快速的演进成为SOA架构 京东服务市场高并发下SOA服务化演进架构 原创: 张俊卿 京东技术 今天

  9. go,gcvis,golang, privoxy,and git proxy

    /etc/init.d/privoxy restartexport http_proxy=http://127.0.0.1:8118export https_proxy=http://127.0.0. ...

  10. [dpdk] SDK编译-简单扼要版

    0. 前提: 环境是CentOS7,archlinux编译有问题,不知道却什么. 1. 解压: [root@dpdk dpdk]# tar Jxf dpdk-2.2.0.tar.xz 2. 设置环境变 ...