0

I am trying to make a Java script where the user can type a command from the Java console in Eclipse, and the command will be sent to the cmd. For example, when you run the program, the console will ask the user what command they want to use, and the command they put in will be sent to the cmd.

John DOE
  • 400
  • 1
  • 3
  • 15
EdtheN3rd
  • 3
  • 1
  • 3
  • 2
    http://stackoverflow.com/questions/5287538/how-to-get-basic-user-input-for-java --> Get the command user typed. http://stackoverflow.com/questions/4157303/how-to-execute-cmd-commands-via-java --> Launch cmd commands in java. – Antoine C. Jun 29 '14 at 17:21
  • I am stuck on the second link. – EdtheN3rd Jun 29 '14 at 18:44

2 Answers2

1
Console console = System.console();
String com = console.readLine("Enter command:");
Process child = Runtime.getRuntime().exec(com);
AHH
  • 902
  • 2
  • 9
  • 22
1

You can use a Scanner ( see this link ) to get the command

Then use Runtime.getRuntime().exec(command); to execute it

Community
  • 1
  • 1
rob
  • 1,276
  • 11
  • 12
  • What I was thinking is if someone would type a command in the Eclipse console, and the command would be sent to the cmd. Thanks for the help! – EdtheN3rd Jun 29 '14 at 18:53