简体   繁体   中英

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:

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

to:

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

and it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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