commands to java codes?
Can u say what u want?
Well, I'd: - Create an Interface named ShellCommand with method execute(); - Implementation would look like this: class PWDCommand implements ShellCommand { String execute() { // current working directory return System.getProperty("user.dir"); } Then create a map that associates command body with it's implementation. Map<String, ShellCommand> commands; Then: commands.put("pwd", new ShellCommand()); All that's left is to make a shell. Scanner scanner = (...) User will enter commands that can be parsed with scanner.get() String command = scanner.get(); Let's check if an entered command exists, and, if yes, execute: ShellCommand cmd = commands.get(command); if (cmd != null) { // Prints result of command execution of entered command System.out.println(cmd.execute()); } That's it. }
By Runtime.execute("your command")
Обсуждают сегодня