需要使用的环境 web3j,nodejs
1 2 3 4 5 6 7 8 | pragma solidity 0.4 . 19 ; contract hello { function main(uint a) constant returns (uint b) { uint result = a * 8 ; return result; } } |
1 2 3 4 | $ solcjs --optimize --bin --abi --output-dir <输出目录> //demo $ solcjs F:\\hello.sol --optimize --bin --abi --output-dir F:\\ |
出现这种错误
1 2 | 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 命令行工具下载
1 2 3 | $ web3j solidity generate <编译的bin文件地址> <编译的abi文件地址> -o <输出目录> -p //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命令,需要进入Release v3.3.1 · web3j/web3j · GitHub网站,下载web3j-3.3.1.tar,并解压。
进入目录bin下,在此目录命令行执行web3j,否则web3j bash命令不存在