5

I'm working on a project, and it will give you a list of Windows commands. When you select one, it will perform that command. However, I don't know how to do that. I was going to do it in Visual C#, or C++, but C++ classes are too complicated, and I don't want to make the forms and junk in Visual C# (really bad at console applications).

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
user2364493
  • 61
  • 1
  • 1
  • 2
  • 1
    Search for "java run command" to help refine the question better - e.g. which part(s) are there issues with? Note that some commands *do not make sense outside of a shell*. These include `cd` and the like and should be emulated accordingly. (Although, I would likely consider it a "better" investment of time to emulate all supported commands - i.e. move/copy/list/delete? - in Java itself or open up a real shell and let the user do whatever they want.) – user2246674 May 09 '13 at 01:37
  • http://stackoverflow.com/questions/7112259/how-to-execute-windows-commands-using-java-change-network-settings – user2246674 May 09 '13 at 01:39
  • Also, if you're *on* Windows, just use VS Express (free) + C# (which is really about the same "difficulty" as Java). It Just Works (TM), including WinForms. – user2246674 May 09 '13 at 01:41
  • https://www.codepuran.com/java/execute-dos-command-java/ – Akshay Pethani May 26 '17 at 13:42

5 Answers5

5

I hope this helps :)

You could use:

Runtime.getRuntime().exec("ENTER COMMAND HERE");
Bevilacqua
  • 465
  • 3
  • 8
  • 19
5

an example. 1. create cmd 2. write to cmd -> call a command.

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) {
}
5

Take advantage of the ProcessBuilder.

It makes it easier to build the process parameters and takes care of issues with having spaces in commands automatically...

public class TestProcessBuilder {

    public static void main(String[] args) {

        try {
            ProcessBuilder pb = new ProcessBuilder("cmd", "/c", "dir");
            pb.redirectError();
            Process p = pb.start();
            InputStreamConsumer isc = new InputStreamConsumer(p.getInputStream());
            isc.start();
            int exitCode = p.waitFor();

            isc.join();
            System.out.println("Process terminated with " + exitCode);
        } catch (IOException | InterruptedException exp) {
            exp.printStackTrace();
        }

    }

    public static class InputStreamConsumer extends Thread {

        private InputStream is;

        public InputStreamConsumer(InputStream is) {
            this.is = is;
        }

        @Override
        public void run() {

            try {
                int value = -1;
                while ((value = is.read()) != -1) {
                    System.out.print((char)value);
                }
            } catch (IOException exp) {
                exp.printStackTrace();
            }

        }

    }
}

I'd generally build a all purpose class, which you could pass in the "command" (such as "dir") and it's parameters, that would append the call out to the OS automatically. I would also included the ability to get the output, probably via a listener callback interface and even input, if the command allowed input...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

This is a sample code to run and print the output of the ipconfig command in the console window.

import java.io.IOException;
import java.io.InputStream;

public class ExecuteDOSCommand {
    public static void main(String[] args) {
        final String dosCommand = "ipconfig";
        try {
            final Process process = Runtime.getRuntime().exec(dosCommand );
            final InputStream in = process.getInputStream();
            int ch;
            while((ch = in.read()) != -1) {
                System.out.print((char)ch);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Source: https://www.codepuran.com/java/execute-dos-command-java/

Shehan Dhaleesha
  • 627
  • 1
  • 10
  • 30
Akshay Pethani
  • 2,390
  • 2
  • 29
  • 42
1

Old question but might help someone passing by. This is a simple and working solution. Some of the above solutions don't work.

import java.io.IOException;
import java.io.InputStream;

public class ExecuteDOSCommand
{
    public static void main(String[] args)
    {
        final String dosCommand = "cmd /c dir /s";
        final String location = "C:\\WINDOWS\\system32";
        try
        {
            final Process process = Runtime.getRuntime().exec(dosCommand + " " + location);
            final InputStream in = process.getInputStream();
            int ch;
            while((ch = in.read()) != -1)
            {
                System.out.print((char)ch);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}

Source: http://www.devx.com/tips/Tip/42644

Swastik Padhi
  • 1,849
  • 15
  • 27