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 ;
}