简体   繁体   English

如何在Java中使用参数运行批处理文件?

[英]How to run batch file with parameters in java?

I have created a jar file called test.jar under C:\\jars. 我在C:\\ jars下创建了一个名为test.jar的jar文件。 I have JAR file under the same location named run.bat and it contains the below code - 我在名为run.bat的相同位置下有一个JAR文件,它包含以下代码-

@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. 现在,我想从另一个JAVA程序运行它。 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. 答案是您必须运行cmd.exe或bash或任何已有的shell,然后通过进程输入/输出流将命令输入该进程。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM