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?

  • 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 May 19 '17 at 18:33
  • @Ismael any idea of how to ship solc with my java app? – forhas Jun 26 '18 at 22:53
  • @forhas It is better if you ask a new question about that. – Ismael Jun 27 '18 at 13:13

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

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)

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()

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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