简体   繁体   English

Java Windows命令无法执行?

[英]Java windows commands not executing?

When I run this program itdisplays the following text instead of the dir dump why? 当我运行该程序时,它显示以下文本而不是目录转储,为什么? This is what it displays. 这就是它显示的内容。

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

It should also display the dir out put but it does now why? 它还应该显示目录输出,但现在为什么呢?

The code 编码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class ExecuteWindowsCommandThread implements Runnable {

    public ExecuteWindowsCommandThread(String command) {
    }

    @Override
    public void run() {
        try {
            final Process p = Runtime.getRuntime().exec("cmd c/ dir");
            //p.waitFor();
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;

            while ((line = input.readLine()) != null) {
                System.out.println(line);
            }
            System.out.println("Done2");

        }

        catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("Done");
    }
}

Don't use c/ instead use /c so, Change: 不要使用c/而是使用/c所以更改:

final Process p = Runtime.getRuntime().exec("cmd c/ dir");

to: 至:

final Process p = Runtime.getRuntime().exec("cmd /c dir");

and it will work. 它会工作。

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

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