Java:运行cmd命令(具有多个参数的多个命令) [英] Java: Running cmd commands (multiple commands with multiple arguments at once)

查看:1059
本文介绍了Java:运行cmd命令(具有多个参数的多个命令)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行cmd(命令行)并以这种方式从Java运行我的批处理文件:

I run cmd (command line) and running my batch file from Java this way:

final String cmd = "cmd /c C: && dir && cd C:\MyApp\Maxi && dir && C:\MayApp\Maxi\deploy.bat";

try {


            Process process = Runtime.getRuntime().exec(cmd);

            final InputStream in = process.getInputStream();

            int ch;

            while((ch = in.read()) != -1) {
                System.out.print((char)ch);
            }
        } catch (IOException e) {
             System.out.println("IOException on CMD executing statement");
             e.printStackTrace();
        }

它工作正常,但是我修改了批处理文件并添加了一些参数,所以我必须传递一个名称到批处理文件,所以我尝试这样:
(我发送Name1作为参数)

It was working successfully, but I modified the batch file and added some argument, so I have to pass a name to the batch file so i tried this: (I send "Name1" as argument)

final String cmd = "cmd /c C: && dir && cd C:\MyApp\Maxi && dir && C:\MayApp\Maxi\deploy.bat Name1";

try {


            Process process = Runtime.getRuntime().exec(cmd);

            final InputStream in = process.getInputStream();

            int ch;

            while((ch = in.read()) != -1) {
                System.out.print((char)ch);
            }
        } catch (IOException e) {
             System.out.println("IOException on CMD executing statement");
             e.printStackTrace();
        }

但它现在不工作,命令不执行。我只得到dir命令输出。

But its not working now and the command is not executed. I only get the "dir" command output.

任何人都可以帮助?


注意:命令在CMD上成功运行,但它不能从
java运行。

Note: The commands run successfully on CMD, but its not working from java.


推荐答案

为什么要在一个命令中执行多个任务?例如更改为C:\,dir然后执行?

Why do you want do multiple tasks in one command? e.g. Change to C:\, dir and then execute?

您可以轻松地将所有这些任务分配到一个批处理文件。

You can easily club all these tasks to one batch file.

这将帮助您不在一些目录结构更改的情况下再次编译代码。

That would help you "not" compiling you code again in case some directory structure changes.

这篇关于Java:运行cmd命令(具有多个参数的多个命令)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆