簡體   English   中英

Java Windows命令無法執行?

[英]Java windows commands not executing?

當我運行該程序時,它顯示以下文本而不是目錄轉儲,為什么? 這就是它顯示的內容。

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

它還應該顯示目錄輸出,但現在為什么呢?

編碼

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");
    }
}

不要使用c/而是使用/c所以更改:

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

至:

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

它會工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM