I have directory /tmp then I need to execute cd and go to that folder. Then I need to execute./executeScript
Preparation
To start solving the problem, I created a directory /home/vulpini99/tmp
. In this directory, I created the bash-script test.sh
, which will open firefox for us:
firefox
Then I created a java-file named LinuxCommand.java
in the directory /home/vulpini99
.
Main part
cd
is just an internal shell command and not an executable program, so I suggest to just use the full path of the bash-script. So the command we want to execute is
bash /home/vulpini99/tmp/test.sh
.
In Java, you can use Runtime
for this purpose:
import java.io.IOException;
public class LinuxCommand {
public static void main(String[] args) {
Runtime run = Runtime.getRuntime();
try {
run.exec("bash /home/vulpini99/tmp/test.sh");
}
catch(IOException e) {
e.printStackTrace();
}
}
}
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.