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
40k7 gold badges94 silver badges124 bronze badges
asked May 9, 2013 at 1:36
4
  • 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.) Commented May 9, 2013 at 1:37
  • stackoverflow.com/questions/7112259/… Commented May 9, 2013 at 1: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. Commented May 9, 2013 at 1:41
  • codepuran.com/java/execute-dos-command-java Commented May 26, 2017 at 13:42

5 Answers 5

5

I hope this helps :)

You could use:

Runtime.getRuntime().exec("ENTER COMMAND HERE");
answered May 9, 2013 at 1:39
Sign up to request clarification or add additional context in comments.

Comments

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) {
}
answered May 9, 2013 at 1:41

Comments

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...

answered May 9, 2013 at 3:04

Comments

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
6261 gold badge11 silver badges32 bronze badges
answered Mar 23, 2017 at 5:27

Comments

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

answered Oct 17, 2015 at 15:14

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.