简体   繁体   中英

Execute source command from Java

I am having a bash script file which I am calling using the source command in a shell and is setting a number of environment variables. Then I can use all the tools the environment variables are setting.

Now I want to do the same in Java by the use of:

static Runtime run = Runtime.getRuntime();
Process pr = run.exec(command);
pr.waitFor();

I know that source is an internal command and I can not call it from Java.

Is there any other way to set the enviroment variable in that file from java in order to be able to use them in my code later for calling other commands?

Thank you in advance!

Process pr = new ProcessBuilder("/bin/bash", "-c", ". env.sh; " + command).start();

Try something like this, where you both source the script and execute a subsequent command in the same shell process. Effectively you source the script every time you want to execute a command.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM