How to execute cmd commands via Java

前端 未结 11 2014
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 13:59

I am trying to execute command line arguments via Java. For example:

// Execute command
String command = \"cmd /c start cmd.exe\";
Process child = Runtime.ge         


        
相关标签:
11条回答
  • 2020-11-22 14:53

    If you want to run several commands in the cmd shell then you can construct a single command like this:

      rt.exec("cmd /c start cmd.exe /K \"cd c:/ && dir\"");
    

    This page explains more.

    0 讨论(0)
  • 2020-11-22 14:53

    As i also faced the same problem and because some people here commented that the solution wasn't working for them, here's the link to the post where a working solution has been found.

    https://stackoverflow.com/a/24406721/3751590

    Also see the "Update" in the best answer for using Cygwin terminal

    0 讨论(0)
  • 2020-11-22 14:54

    The code you posted starts three different processes each with it's own command. To open a command prompt and then run a command try the following (never tried it myself):

    try {
        // Execute command
        String command = "cmd /c start cmd.exe";
        Process child = Runtime.getRuntime().exec(command);
    
        // Get output stream to write from it
        OutputStream out = child.getOutputStream();
    
        out.write("cd C:/ /r/n".getBytes());
        out.flush();
        out.write("dir /r/n".getBytes());
        out.close();
    } catch (IOException e) {
    }
    
    0 讨论(0)
  • 2020-11-22 15:02

    I found this in forums.oracle.com

    Allows the reuse of a process to execute multiple commands in Windows: http://kr.forums.oracle.com/forums/thread.jspa?messageID=9250051

    You need something like

       String[] command =
        {
            "cmd",
        };
        Process p = Runtime.getRuntime().exec(command);
        new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
        new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
        PrintWriter stdin = new PrintWriter(p.getOutputStream());
        stdin.println("dir c:\\ /A /Q");
        // write any other commands you want here
        stdin.close();
        int returnCode = p.waitFor();
        System.out.println("Return code = " + returnCode);
    

    SyncPipe Class:

    class SyncPipe implements Runnable
    {
    public SyncPipe(InputStream istrm, OutputStream ostrm) {
          istrm_ = istrm;
          ostrm_ = ostrm;
      }
      public void run() {
          try
          {
              final byte[] buffer = new byte[1024];
              for (int length = 0; (length = istrm_.read(buffer)) != -1; )
              {
                  ostrm_.write(buffer, 0, length);
              }
          }
          catch (Exception e)
          {
              e.printStackTrace();
          }
      }
      private final OutputStream ostrm_;
      private final InputStream istrm_;
    }
    
    0 讨论(0)
  • 2020-11-22 15:02

    Writing to the out stream from the process is the wrong direction. 'out' in that case means from the process to you. Try getting/writing to the input stream for the process and reading from the output stream to see the results.

    0 讨论(0)
提交回复
热议问题