/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package useprocessbuilderfx; import java.io.File; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.nio.charset.Charset; import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.TreeSet; import java.util.logging.Level; import java.util.logging.Logger; 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; /** * * @author yjoan */ public class UseProcessBuilderFX extends Application { @Override public void start(Stage primaryStage) { Button btn = new Button(); btn.setText("Reveal '/Users' in Finder"); btn.setOnAction(new EventHandler() { @Override public void handle(ActionEvent event) { open("/Users"); } }); StackPane root = new StackPane(); root.getChildren().add(btn); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("ProcessBuilder"); primaryStage.setScene(scene); primaryStage.show(); } /** * The main() method is ignored in correctly deployed JavaFX application. * main() serves only as fallback in case the application can not be * launched through deployment artifacts, e.g., in IDEs with limited FX * support. NetBeans ignores main(). * * @param args the command line arguments */ public static void main(String[] args) { launch(args); } private static void executeDaemon(List cmd, File wDir) { try { ProcessBuilder builder = new ProcessBuilder(cmd); builder = builder.directory(wDir); // builder.environment().put("PWD", "/"); // builder.environment().put("COMMAND_MODE", "unix2003"); // builder.environment().put("LC_CTYPE", "UTF-8"); // builder.environment().put("SHLVL", "1"); File outFile = new File("/tmp/executeDaemon"); TreeSet content = new TreeSet<>(); String command = ""; for (String titi : builder.command()) { command += titi + " "; } content.add("\nexecuteDaemon - Command of the ProcessBuilder is: " + command); content.add("\nexecuteDaemon - Directory of the ProcessBuilder is " + builder.directory()); content.add("\nexecuteDaemon - Environment of the ProcessBuilder is:"); for (String toto : builder.environment().keySet()) { content.add(toto + " = " + builder.environment().get(toto)); } Files.write(outFile.toPath(), content, Charset.forName("UTF-8")); builder.start(); } catch (Exception ex) { File outFile = new File("/tmp/executeDaemon.stack"); TreeSet content = new TreeSet<>(); content.add(ex.getMessage()); StringWriter writer = new StringWriter(); ex.printStackTrace(new PrintWriter(writer, true)); content.add(writer.getBuffer().toString()); try { Files.write(outFile.toPath(), content, Charset.forName("UTF-8")); } catch (IOException ex1) { } } } public static void open(String path) { List args = new ArrayList<>(); args.add("open"); args.add("-R"); args.add(path); if (!args.isEmpty()) { executeDaemon(args, null); } } }