Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在windows上正确调用JAVA中的CMD文件?_Java_Process_Processbuilder - Fatal编程技术网

如何在windows上正确调用JAVA中的CMD文件?

如何在windows上正确调用JAVA中的CMD文件?,java,process,processbuilder,Java,Process,Processbuilder,我有如下示例java代码 String testEfdDirectoryPath="D:\\test"; String efdExecutable = "test.cmd"; File executableFile = new File(testEfdDirectoryPath, efdExecutable); ProcessBuilder pb=new ProcessBuilder(); $$pb.command("cmd.exe","/C",execu

我有如下示例java代码

    String testEfdDirectoryPath="D:\\test";
    String efdExecutable = "test.cmd";
    File executableFile = new File(testEfdDirectoryPath, efdExecutable);
    ProcessBuilder pb=new ProcessBuilder();
    $$pb.command("cmd.exe","/C",executableFile.toString());$$
    pb.directory(new File(testEfdDirectoryPath));
    Process p=pb.start();
    int code=p.waitFor();
    System.out.print(code);
在test.cmd中,实际上有一个对另一个java应用程序的调用。除非我将$$标记行更改为以下内容以重定向其输出,否则无法启动另一个java应用程序

    pb.command("cmd.exe","/C",executableFile.toString(),">output.txt");

你们有什么想法吗?提前感谢。:)

Windows无法直接执行脚本;双击.cmd文件时,实际上会在cmd.exe中打开该文件。因此,请尝试
cmd.exe E:\\test\\test.cmd

您的子进程是否会产生大量输出(超过几千字节)?如果是这种情况,您需要从流程中读取该输出。你应该试试:

  • 开始这个过程
  • 关闭进程的
    stdin
    ,因此
    pb.getOutputStream().close()
  • 重复读取
    pb.getInputStream()
    和错误流
  • 这可能在一个线程中实现,也可能在多个线程中实现。无论如何,您应该将上面的解释作为一个关键字列表,并尝试搜索一个您可以信任的示例代码段,最好是从成功完成这类工作的开源应用程序中搜索


    也许我能帮你。

    谢谢,罗兰。你是对的。我发现ProcessBuilder的javadoc中说明了这个问题。