3

I want to compile a smart contract (solidity code) in Java. Currently I am using the web3j library. When I run the method web3j.ethCompileSolidity(contract) I'm getting the following error: The method eth_compileSolidity does not exist/is not available

I created the following code to compile a smart contract at runtime:

EthCompileSolidity ethCompileSolidity = web3j.ethCompileSolidity(contract).send();
Map<String, EthCompileSolidity.Code> compiledSolidity = ethCompileSolidity.getCompiledSolidity();

The method web3j.ethGetCompilers() doesn't work either. It's returning the following error: The method eth_compileSolidity does not exist/is not available

On Github I read something about deprecated eth_compile methods.

Is there still a way to compile smart contracts in Java using this library?

3
  • 1
    Probably the smart move is to ship the compiler solc with your application and then shell to it to compile. Another alternative (if you support node/javascript) to ship the javascript package solc, but it is less performant.
    – Ismael
    Commented May 19, 2017 at 18:33
  • @Ismael any idea of how to ship solc with my java app?
    – forhas
    Commented Jun 26, 2018 at 22:53
  • @forhas It is better if you ask a new question about that.
    – Ismael
    Commented Jun 27, 2018 at 13:13

2 Answers 2

2

You are right is's because of deprecated eth_compile

There is no easy way to solve it. You can:

  • downgrade geth version on your dev machine
  • create a custom version of geth with a diff and keep it in your branch
0

I got it by running solc executable from my java code (I'm running windows os). I downloaded solc.exe from https://github.com/ethereum/solidity/releases, put it in my classpath and used the following code:

List<String> commandParts = Lists.newArrayList("path.to.solc.exe", "--bin", "path.to.sol.file");
//loading solc.exe file
InputStream fileURL = MyClass.class.getResourceAsStream(pathToFile)
solcFile = File.createTempFile("solc", "exe");
FileUtils.copyInputStreamToFile(fileURL, solcFile); // FileUtils of org.apache.commons.io
ProcessBuilder processBuilder = new ProcessBuilder(commandParts)
        .directory(executable.getParentFile());
processBuilder.environment().put("LD_LIBRARY_PATH",
        executable.getParentFile().getCanonicalPath());

Process process = processBuilder.start();

Remember to close your stream (fileURL), compilation output is taken from process.getInputStream(), if compilation fails errors are taken from process.getErrorStream()

1
  • I am unable to understand the few contexts of the code, Could you publish the complete code with explanation?
    – kaushik_pm
    Commented Oct 18, 2018 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.