Javafx mac terminal external process

265 views Asked by At

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

There are 1 answers

3
Oo.oO On

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);
    }
}