简体   繁体   中英

How to run batch file with parameters in java?

I have created a jar file called test.jar under C:\\jars. I have JAR file under the same location named run.bat and it contains the below code -

@echo off

set exec_path=C:\jars java -cp %exec_path%/test.jar; com.mycomp.myapp.MyProgram "%1"%*

@echo on

It is running successfully from command prompt with parameters.

Now I would like to run it from another JAVA program. Please suggest.

Thanks!

I've encountered this issue before. The answer is that you have to run cmd.exe or bash or whatever shell you've got, then feed in the command to that process via the process input/output streams.

Process p = Runtime.getRuntime().exec("cmd");
p.getOutputStream().write("mybatch.bat\n");

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