简体   繁体   中英

Call bash script in Java program with export variables

I need to call a bash script in java class . Before calling the bash script , few variables need to be exported. So when bash script is executed. It's should get the required exported values.

class Javapgm {

public static void main(String [] args){

//export VAR=10

//Call bash script script.bash so that it can use this VAR

}

}

To execute a bash script, use ProcessBuilder .
To set environment variables, call environment() .

ProcessBuilder pb = new ProcessBuilder("/bin/sh", "-c", "script.bash");
pb.inheritIO();
pb.environment().put("VAR", "10");
Process p = pb.start();
p.waitFor();

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