简体   繁体   English

从JavaFx运行Perl脚本

[英]Running Perl Script from JavaFx

I'm trying to run a Perl script from a JavaFx stage when a button is pressed. 我试图在按下按钮时从JavaFx阶段运行Perl脚本。

I'm able to use Runtime.getRuntime().exec() to run the script in the public static void main(String[] args) method of the main class, but when doing this it doesn't run until the Application.launch(args) is completed. 我可以使用Runtime.getRuntime().exec()在主类的public static void main(String[] args)方法中运行脚本,但是这样做时,它直到Application.launch(args)才运行Application.launch(args)完成。 I want it to run while the stage is still active instead of having to close it. 我希望它在舞台仍处于活动状态时运行,而不必关闭它。

I tried adding it to the ButtonHandlerClass as part of the public void handle(ActionEvent e) method with no luck. 我尝试将其作为public void handle(ActionEvent e)方法的一部分添加到ButtonHandlerClass中,但是没有运气。

Using the following perl script (windows perl...) where I can extend the sleep call to make it take longer the UI stays responsive while perl is doing it's thing, then the text area is updated with the string returned from perl. 使用以下perl脚本(windows perl ...),在其中我可以扩展sleep调用,以使其花费更长的时间,而在执行perl时,UI保持响应状态,然后用从perl返回的字符串更新文本区域。

ETA: Probably should have put the readers in a try-with-resources and closed them up... ETA:可能应该让读者尝试一下资源,然后关闭它们……

my $x = '';
for (my $i=0; $i <= 1000000; $i++) {
    if ($i % 100000 == 0) {
        sleep(1);
        $x .= '> ' . $i . "\n";
    }
}
print $x;

And JavaFX 和JavaFX

public class PTApp extends Application {

    private TextArea ta;
    private Label lblMsg;


    @Override
    public void start(Stage primaryStage) {

        VBox vb = new VBox(4);
        vb.setPadding(new Insets(4));
        Button btn = new Button();
        btn.setText("Run Script");
        btn.setOnAction((ActionEvent event) -> {
            runPerlScript();
        });

        lblMsg = new Label("Ready...");

        ta = new TextArea();
        VBox.setVgrow(ta, Priority.ALWAYS);

        vb.getChildren().addAll(btn, lblMsg, ta);

        Scene scene = new Scene(vb, 600, 650);

        primaryStage.setTitle("Perl Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }


    private void runPerlScript() {
        ta.setText("About to begin...");

        PerlTask pt = new PerlTask();
        lblMsg.textProperty().bind(pt.messageProperty());
        pt.stateProperty().addListener((obs, os, ns) -> {
            //System.out.println("Worker state changed: " + ns.toString());
            if (Worker.State.SUCCEEDED.equals(ns)) {
                Platform.runLater(() -> {
                    ta.setText(pt.getValue());
                });
            } else if (Worker.State.FAILED.equals(ns)) {
                ta.setText(pt.getException().getMessage());
            }
        });

        Thread th = new Thread(pt);
        th.start();

    }


    public static class PerlTask extends Task<String> {

        @Override
        protected String call() throws Exception {

            updateMessage("About to call Perl...");

            Process pp = Runtime.getRuntime().exec("C:\\Perl64\\bin\\perl.exe D:\\PerlTest\\perl\\test.pl");

            updateMessage("Perl called, building readers...");

            BufferedReader br = new BufferedReader(new InputStreamReader(pp.getInputStream()));
            BufferedReader brE = new BufferedReader(new InputStreamReader(pp.getErrorStream()));

            StringBuilder sb = new StringBuilder("--------Perl Output\n");
            String l;
            while ((l = br.readLine()) != null) {
                sb.append(l).append("\n");
            }

            updateMessage("Input stream read...");
            sb.append("\n");
            sb.append("--------Perl Errors\n");
            while ((l = brE.readLine()) != null) {
                sb.append(l).append("\n");
            }

            updateMessage("Error stream read...");

            return sb.toString();

        }

    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM