solcJ
提供了Java
编译solidity
合约文件的接口,solidity
编译器solc
可以参考: https://github.com/ethereum/solidity
solcJ
支持三个版本的solidity
编译器版本
Gradle
# 0.4.25 版本
compile group: 'org.fisco-bcos', name: 'solcJ', version: '0.4.25.0'
# 0.5.2 版本
compile group: 'org.fisco-bcos', name: 'solcJ', version: '0.5.2.0'
# 0.6.10 版本
compile group: 'org.fisco-bcos', name: 'solcJ', version: '0.6.10.0'
Maven
# 0.4.25 版本
<dependency>
<groupId>org.fisco-bcos</groupId>
<artifactId>solcJ</artifactId>
<version>0.4.25.0</version>
</dependency>
# 0.5.2 版本
<dependency>
<groupId>org.fisco-bcos</groupId>
<artifactId>solcJ</artifactId>
<version>0.5.2.0</version>
</dependency>
# 0.6.10 版本
<dependency>
<groupId>org.fisco-bcos</groupId>
<artifactId>solcJ</artifactId>
<version>0.6.10.0</version>
</dependency>
public static Result SolidityCompiler::compile(File source, boolean sm, boolean combinedJson, Option... options)
throws IOException;
public static Result SolidityCompiler::compile(byte[] source, boolean sm, boolean combinedJson, Option... options)
throws IOExceptio;
参数:
solidity
文件文件路径solidity
文件内容 solc
编译参数
Options.ABI
: 相当于solc --abi
Options.BIN
: 相当于solc --bin
Options.INTERFACE
: 相当于solc --interface
Options.METADATA
: 相当于solc --metadata
Options.ASTJSON
: 相当于solc --ast-json
返回:
SolidityCompiler::Result
对象:
solc
的返回格式public static CompilationResult CompilationResult::parse(String rawJson) throws IOException
参数:
返回:
File socFile = new File("HelloWorld.sol");
SolidityCompiler.Result res = SolidityCompiler.compile(solFile, false, true, ABI, BIN, INTERFACE, METADATA);
if (!res.isFailed())) {
// 编译成功,解析返回
CompilationResult result = CompilationResult.parse(res.getOutput());
// 获取HelloWorld合约的编译结果
CompilationResult.ContractMetadata meta = result.getContract(“HelloWorld”);
// abi
String abi = meta.abi;
// bin
String bin = meta.bin;
// 其他逻辑
} else {
// 编译失败,res.getError()保存编译失败的原因
}