How to get bytecode in Java (all programmatically)?

By May 19, 2020Ethereum
Click here to view original web page at ethereum.stackexchange.com

I am trying to generate a bytecode to deploy a contract to ethereum. I have included node.exe as resource and try to install solcjs programmatically in node, but unfortunately i am stuck.

My current code to generate a bytecode by solc js is; (below)

What is the simplest way of generating bytecode on linux and windows (with same code). I have web3j included and try to use solc js, since i could not find solc.exe to include as resources in java to execute directly in cmd.

        String UUID = __SolidityFile.getValue(getContext(),"__UUID__");
        String firstFolder = UUID.substring(0, 2);
        String secondFolder = UUID.substring(2, 4);
        String secondFolderLocation=MxProjectLocation+"deployment\\data\\files\\" + firstFolder+ "\\" + secondFolder ;
        String fileLocation=MxProjectLocation+"deployment\\data\\files\\" + firstFolder+ "\\" + secondFolder+"\\"+ UUID;

        final File file = new File(secondFolderLocation+"\\compile.bat");
        file.createNewFile();
        PrintWriter writer = new PrintWriter(file, "UTF-8");
        writer.println("cd "+secondFolderLocation);
        Core.getLogger("Solidity").info("cd "+secondFolderLocation);
        writer.println("solcjs " + UUID+" --bin");
        Core.getLogger("Solidity").info("solcjs " + UUID+" --bin");
        writer.println("exit");
        Core.getLogger("Solidity").info("exit");
        writer.close();
        Process p =  Runtime.getRuntime().exec("cmd /c compile.bat", null, new File(secondFolderLocation));

        Core.getLogger("Solidity").info("bat file executed");
        p.waitFor();
        file.delete();
        Core.getLogger("Solidity").info("bat file deleted");

        String byteCodeFile = "";
        String byteFileType = ByteCodeType.getCaption();

        switch (byteFileType) {
        case "TestToken": 
             byteCodeFile =  fileLocation+"_TESTTOKEN.bin" ;
             break;

        case "SafeMath": 
             byteCodeFile =  fileLocation+"_SafeMath.bin" ;
             break;

        case "Owned": 
             byteCodeFile =  fileLocation+"_Owned.bin" ;
             break;

        case "ERC20Interface": 
             byteCodeFile =  fileLocation+"_ERC20Interface.bin" ;
             break;

        case "ApproveAndCallFallBack": 
             byteCodeFile =  fileLocation+"_ApproveAndCallFallBack.bin" ;
             break;  

        }
        Core.getLogger("Solidity").info(byteCodeFile);


        try {
              //  Block of code to try
            Scanner scanner = new Scanner( new File(byteCodeFile), "UTF-8" );
            String text = scanner.useDelimiter("\\A").next();
            scanner.close();
            return text;

            }
            catch(Exception e) {
              //  Block of code to handle errors
                return null;
            }



        ```

Leave a Reply