Run cmd commands through Java

2018-12-31 20:42发布

问题:

I found several code snippets for running cmd commands through a Java class, but I wasn\'t able to understand it.

This is code for opening the cmd

public void excCommand(String new_dir){
    Runtime rt = Runtime.getRuntime();
    try {
        rt.exec(new String[]{\"cmd.exe\",\"/c\",\"start\"});

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

And I found some other links for adding other commands such as cd http://www.coderanch.com/t/109753/Linux-UNIX/exec-command-cd-command-java

How to open the command prompt and insert commands using Java?

Can anyone help me to understand how to cd a directory such as:

 cd C:\\Program Files\\Flowella

then run other commands on that directory?

回答1:

One way to run a process from a different directory to the working directory of your Java program is to change directory and then run the process in the same command line. You can do this by getting cmd.exe to run a command line such as cd some_directory && some_program.

The following example changes to a different directory and runs dir from there. Admittedly, I could just dir that directory without needing to cd to it, but this is only an example:

import java.io.*;

public class CmdTest {
    public static void main(String[] args) throws Exception {
        ProcessBuilder builder = new ProcessBuilder(
            \"cmd.exe\", \"/c\", \"cd \\\"C:\\\\Program Files\\\\Microsoft SQL Server\\\" && dir\");
        builder.redirectErrorStream(true);
        Process p = builder.start();
        BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line;
        while (true) {
            line = r.readLine();
            if (line == null) { break; }
            System.out.println(line);
        }
    }
}

Note also that I\'m using a ProcessBuilder to run the command. Amongst other things, this allows me to redirect the process\'s standard error into its standard output, by calling redirectErrorStream(true). Doing so gives me only one stream to read from.

This gives me the following output on my machine:

C:\\Users\\Luke\\StackOverflow>java CmdTest
 Volume in drive C is Windows7
 Volume Serial Number is D8F0-C934

 Directory of C:\\Program Files\\Microsoft SQL Server

29/07/2011  11:03    <DIR>          .
29/07/2011  11:03    <DIR>          ..
21/01/2011  20:37    <DIR>          100
21/01/2011  20:35    <DIR>          80
21/01/2011  20:35    <DIR>          90
21/01/2011  20:39    <DIR>          MSSQL10_50.SQLEXPRESS
               0 File(s)              0 bytes
               6 Dir(s)  209,496,424,448 bytes free


回答2:

You can try this:-

Process p = Runtime.getRuntime().exec(command);


回答3:

If you want to perform actions like cd, then use:

String[] command = {command_to_be_executed, arg1, arg2};
ProcessBuilder builder = new ProcessBuilder(command);
builder = builder.directory(new File(\"directory_location\"));

Example:

String[] command = {\"ls\", \"-al\"};
ProcessBuilder builder = new ProcessBuilder(command);
builder = builder.directory(new File(\"/ngs/app/abc\"));
Process p = builder.start();

It is important that you split the command and all arguments in separate strings of the string array (otherwise they will not be provided correctly by the ProcessBuilder API).



回答4:

My example (from real project)

folder — File.

zipFile, filesString — String;

        final String command = \"/bin/tar -xvf \" + zipFile + \" \" + filesString;
        logger.info(\"Start unzipping: {}    into the folder {}\", command, folder.getPath());
        final Runtime r = Runtime.getRuntime();
        final Process p = r.exec(command, null, folder);
        final int returnCode = p.waitFor();

        if (logger.isWarnEnabled()) {
            final BufferedReader is = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = is.readLine()) != null) {
                logger.warn(line);
            }
            final BufferedReader is2 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((line = is2.readLine()) != null) {
                logger.warn(line);
            }
        }


回答5:

The easiest way would be to use Runtime.getRuntime.exec().

For example, to get a registry value for the default browser on Windows:

String command = \"REG QUERY HKEY_CLASSES_ROOT\\\\http\\\\shell\\\\open\\\\command\";
try
{
    Process process = Runtime.getRuntime().exec(command);
} catch (IOException e)
{
    e.printStackTrace();
}

Then use a Scanner to get the output of the command, if necessary.

Scanner kb = new Scanner(process.getInputStream());

Note: the \\ is an escape character in a String, and must be escaped to work properly (hence the \\\\).


However, there is no executable called cd, because it can\'t be implemented in a separate process.

The one case where the current working directory matters is executing an external process (using ProcessBuilder or Runtime.exec()). In those cases you can specify the working directory to use for the newly started process explicitly.

Easiest way for your command:

System.setProperty(\"user.dir\", \"C:\\\\Program Files\\\\Flowella\");


回答6:

Here is a more complete implementation of command line execution.

Usage

executeCommand(\"ls\");

Output:

12/27/2017 11:18:11:732: ls
12/27/2017 11:18:11:820: build.gradle
12/27/2017 11:18:11:820: gradle
12/27/2017 11:18:11:820: gradlew
12/27/2017 11:18:11:820: gradlew.bat
12/27/2017 11:18:11:820: out
12/27/2017 11:18:11:820: settings.gradle
12/27/2017 11:18:11:820: src

Code

private void executeCommand(String command) {
    try {
        log(command);
        Process process = Runtime.getRuntime().exec(command);
        logOutput(process.getInputStream(), \"\");
        logOutput(process.getErrorStream(), \"Error: \");
        process.waitFor();
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

private void logOutput(InputStream inputStream, String prefix) {
    new Thread(() -> {
        Scanner scanner = new Scanner(inputStream, \"UTF-8\");
        while (scanner.hasNextLine()) {
            synchronized (this) {
                log(prefix + scanner.nextLine());
            }
        }
        scanner.close();
    }).start();
}

private static SimpleDateFormat format = new SimpleDateFormat(\"MM/dd/yyyy hh:mm:ss:SSS\");

private synchronized void log(String message) {
    System.out.println(format.format(new Date()) + \": \" + message);
}


回答7:

Once you get the reference to Process, you can call getOutpuStream on it to get the standard input of the cmd prompt. Then you can send any command over the stream using write method as with any other stream.

Note that it is process.getOutputStream() which is connected to the stdin on the spawned process. Similarly, to get the output of any command, you will need to call getInputStream and then read over this as any other input stream.



回答8:

You can\'t run cd this way, because cd isn\'t a real program; it\'s a built-in part of the command-line, and all it does is change the command-line\'s environment. It doesn\'t make sense to run it in a subprocess, because then you\'re changing that subprocess\'s environment — but that subprocess closes immediately, discarding its environment.

To set the current working directory in your actual Java program, you should write:

System.setProperty(\"user.dir\", \"C:\\\\Program Files\\\\Flowella\");


回答9:

Try this:

Process runtime = Runtime.getRuntime().exec(\"cmd /c start notepad++.exe\");


回答10:

The simplest and shortest way is to use CmdTool library.

new Cmd()
         .configuring(new WorkDir(\"C:/Program Files/Flowella\"))
         .command(\"cmd.exe\", \"/c\", \"start\")
         .execute();

You can find more examples here.



标签: java cmd cd