简体   繁体   中英

How to cd and execute an executable in ubuntu through java program

Hello and excuse me for being new to java coding, but what I am trying to do is a java program that calls an executable program with some given parameters in ubuntu. I've found the code above in another stackoverflow question:

ProcessBuilder pb = new ProcessBuilder();
pb.command("bash", "-c", "./runCalculator.sh");
Process process = pb.start();
int retValue = process.waitFor();

But how can I cd to the executable file first and then execute the program, displaying its output, through java? Thank you.

You don't have to cd anywhere, just specify the absolute path.

String path = "/home/Omen/runCalculator.sh";
pb.command("bash", "-c", path);

You have 2 options:

Use absolute path

pb.command("bash", "-c", "/path/to/runCalculator.sh");

Use ProcessBuilder directory method:

pb.directory(new File("/path/to"));

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