简体   繁体   中英

Inputting a command to cmd with Java

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.

Console console = System.console();
String com = console.readLine("Enter command:");
Process child = Runtime.getRuntime().exec(com);

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM