I am trying to write a Java code which can be used on Windows and Linux machine to generate bytecode for Ethereum contracts.
I am not familiar with shell scripts and linux, from what I read on internet I would be able to execute the following batch file via bash...but unfortunately i got it not working. What is the problem with my code which is meant for windows and linux and generates a batch file for windows and a shell file for linux.
How should the code be for linux? On windows this code works like a charm.
String delim="";
String scriptName="";
String cmd="";
if (Linux) {
delim = "/";
scriptName="compile.sh";
cmd = "bash /c compile.sh";
}
else {
delim = "\\";
scriptName="compile.bat";
cmd = "cmd /c compile.bat";
}
String UUID = __SolidityFile.getValue(getContext(),"__UUID__");
String firstFolder = UUID.substring(0, 2);
String secondFolder = UUID.substring(2, 4);
String secondFolderLocation=com.mendix.core.Core.getConfiguration().getBasePath()+delim+"data"+delim+"files"+delim + firstFolder+ delim + secondFolder ;
String fileLocation=secondFolderLocation+ delim + UUID;
Core.getLogger("Solidity").info(fileLocation);
String solc =com.mendix.core.Core.getConfiguration().getResourcesPath()+delim+"node"+delim+"node_modules"+delim+"solc"+delim+"solcjs";
String node=com.mendix.core.Core.getConfiguration().getResourcesPath()+delim+"node.exe";
try {
// Block of code to try
final File file = new File(secondFolderLocation+delim+scriptName);
file.createNewFile();
PrintWriter writer = new PrintWriter(file, "UTF-8");
writer.println("cd "+secondFolderLocation);
writer.println(node+" "+ solc +" " +UUID +" --bin");
writer.println("exit");
writer.close();
Process p = Runtime.getRuntime().exec(cmd, null, new File(secondFolderLocation));
p.waitFor();
//file.delete();
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);
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 ;
}
Try do it:
When is Linux environment:
Remove bash /c
cmd = "/usr/local/bin/compile.sh";
You need another environment condition(if) to run the code below:
ProcessBuilder pb = new ProcessBuilder(cmd);
Process p = pb.start();
Please, try it and let me know
My code
File opt/development/workspace/testes/src/testes/Main.java
package testes;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
String cmd = "/opt/development/workspace/testes/src/testes/script.sh";
ProcessBuilder pb = new ProcessBuilder(cmd);
Process p = pb.start();
System.out.println(p);
}
}
File script.sh
mkdir stackoverfllow
The folder has been created in /opt/development/workspace/testes
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.