简体   繁体   中英

How to use Java to start a bat file on windows and sh file on linux to run from a specific directory

I am trying to make a program which will execute a bat file when run on windows and a sh file when run on linux, but I need the bash/sh file to start running from a specific directory.

For example: My program is located at C:\\Program\\test.jar My bash file is located at C:\\File\\start.bat I want to make the java runnable execute the start.bat file from its folder and not test.jar running directory.

I tried using Runtime.getRuntime().exec("cmd /C start \\"\\" \\"C:\\File\\start.bat""); and it works, but it runs the bat file from C:\\Program\\test.jar , and for what the bat file does, I need it to run from C:\\File\\start.bat .

I also need to do the same for Linux, so I am asking here because after the bat issue is fixed, this issue will likely stomp me as well.

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