Why does the following code print false? I am trying to change an environment variable in test.sh script and compile it in java. Please suggest an alternative approach if possible.
public static void main(String[] args){ ProcessBuilder processBuilder = new ProcessBuilder("test.sh"); Process process; int exitCode; try { process = processBuilder.start(); exitCode = process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } Map<String, String>envVars = processBuilder.environment(); System.out.println(envVars.keySet().contains("SOURCE")); }
And the code for test.sh script is just
set SOURCE=source
The ProcessBuilder.environment() method is used to transfer the source environment to the process when start() called. You cannot get the subprocess environment from the parent process. This is not a Java limitation: you cannot even get the subprocess environment from the Bash script shell (or actually anything) that creates the subprocess. You need to find another means to pass information from the subprocess back to the parent process.
ProcessBuilder.environment()
start()
In my opinion, you should change:
ProcessBuilder processBuilder = new ProcessBuilder("test.sh");
to
ProcessBuilder processBuilder = new ProcessBuilder("/bin/bash", "test.sh"); processBuilder.directory(new File(the-dir-of-test.sh));
Source: https://habr.com/ru/post/1266575/More articles:Property does not exist in type 'object' - typescriptI canβt download the signed APK on the PlayStore - androidjavascript overflow overflow - javascriptHow reloadOnChange from Microsoft.Extensions.Configuration works for appsettings.json - jsoncmd.exe hangs unexpectedly depending on where the file I'm using is located - javaMongoDB: error with $ divide and $ multiply - mongodbVisual studio 2017 not enough areas - asp.net-mvchttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1266578/can-i-use-javascript-or-css-to-close-a-previous-div&usg=ALkJrhiZjju7w9pU3D0SVezCUh8jr6A4iwangular 2 pass array to custom validator (template driven form) - validationUnable to set 'dir' undefined ckeditor property in Angular2 - javascriptAll Articles