0

How to launch terminal and execute some commands using java code in MAC?

Similar question i have found for LINUX OS from below link.

how to run a command at terminal from java program?

Community
  • 1
  • 1
Gobi M
  • 3,243
  • 5
  • 32
  • 47

2 Answers2

0

You need to run it using bash executable like this:

Runtime.getRuntime().exec("/bin/bash -c **YouTerminalSoftWareName**");

YouTerminalSoftWareName must be absolute path or aready in your PATH environment variable

Prasad Khode
  • 6,602
  • 11
  • 44
  • 59
  • 3
    Runtime.getRuntime().exec("/bin/bash -c /Applications/Utilities/Terminal.app"); I am doing like this but not able to open terminal – SaiPawan Nov 14 '16 at 07:14
0

Use Apple Script Editor to open Terminal and run your code.

For Example :

String action1 = jsonDriver.readApplicationDetails("OFF");
Runtime runtime = Runtime.getRuntime();
String applescriptCommand = "tell application \"Terminal\"\n" + 
         "activate\n" + "delay 1\n" + "tell application \"System Events\"\n" + 
         "keystroke \"" + action1 + "\"\n" + "end tell\n" + "end tell"; 
String[] args1 = { "osascript", "-e", applescriptCommand };
Process process = runtime.exec(args1);
Thread.sleep(5000);
Bhushan
  • 1,489
  • 3
  • 27
  • 45