0

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

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”作为参数)

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”命令输出。

任何人都可以帮忙吗?

注意:命令在 CMD 上成功运行,但在 java 中无法运行。

4

1 回答 1

0

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

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

这将帮助您“不”再次编译您的代码,以防某些目录结构发生变化。

于 2013-06-12T18:55:02.667 回答