0

Using JavaFX, I am trying to run a program a.out on Terminal of Mac OSX. The following code does not work on Mac. The same code works on Windows by writing cmndM={"cmd","/c","start","a.exe"}. What's wrong on Mac?

protected void onRunClick(ActionEvent evt) throws IOException {
    String exeM="./a.out";
    Runtime runtime=Runtime.getRuntime();
    String[] cmndM= {"/bin/sh","-c",exeM}; Process pm=null; File dirM=new File(pth);
    try {
        pm=runtime.exec(cmndM, null, dirM);}
    }catch (IOException e) {
        msg.setText("Error in running simulation.");
    }
}

1 Answers1

0

It might be related to location of the file.

I'd have tried with fixed location first, just to be sure it works.

Also, make sure the file is actually there, inside pth.

package javafxapplication1;

import java.io.File;
import java.io.IOException;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class JavaFXApplication1 extends Application {

    @Override
    public void start(Stage primaryStage) {
        Button btn = new Button();
        btn.setText("Say 'Hello World'");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                String exeM="/tmp/a.out";
                Runtime runtime=Runtime.getRuntime();
                String[] cmndM= {"/bin/sh","-c",exeM}; 
                Process pm=null; 
                File dirM=new File("/tmp");
                try {
                    pm=runtime.exec(cmndM, null, dirM);
                }catch (IOException e) {
                    System.out.println("Error");
                }
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
Oo.oO
  • 12,464
  • 3
  • 23
  • 45
  • I have modified the code by the suggestion, and a.out ran successfully. But, the Terminal does not appear on the screen. How can I make screen visible? – Ted Carter Jan 18 '19 at 12:48
  • You are looking for something like this `open -b com.apple.terminal /tmp/a.out` instead of running `/bin/bash` or `/bin/sh` – Oo.oO Jan 18 '19 at 13:07
  • 'cmndM= {"open","-b","com.apple.terminal"};' could open theTeminal. But 'cmndM= {"open","-b","com.apple.terminal","-c",exeM};' couldn't, and happened nothing. If I use 'cmndM= {"open","-b","com.apple.terminal",exeM};', 'Segmentation fault: 11' occured. I cannnot run a.out on visible Terminal yet. – Ted Carter Jan 19 '19 at 06:23