JavaFX z Arią - powrót do strony głównej

jrunscript – uruchamianie z klasy Java bez cmd

Korzystanie z wiersza poleceń (cmd) jest często frustrujące.
jrunscript można uruchamiać z klasy Java bez uruchamiania wiersza poleceń. A oto dwa przykłady:

Klasa PB_jrunscript1.java
package jsr223groovy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PB_jrunscript1 {
    public static void main(String[] args) {
        try {
            ProcessBuilder pb = new ProcessBuilder();

            pb.command("jrunscript",
                    "-cp", "C:/groovy-4.0.6/lib/groovy-jsr223-4.0.6.jar;C:/groovy-4.0.6/lib/groovy-4.0.6.jar",
                    "-l","groovy",
                    "-q");
            pb.redirectError(ProcessBuilder.Redirect.INHERIT);
            Process p = pb.start();
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = r.readLine()) != null) {
                System.out.println("linia: "+ line);
            }
            int exitCode = p.waitFor();
            System.out.println("\nExited with code : " + exitCode);
            r.close();
            p.destroy();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Po uruchomieniu klasy na konsoli zobaczymy:

Language Groovy 4.0.6 implementation "Groovy Scripting Engine" 2.0

Exited with code : 0
Klasa PB_jrunscript2.java
package jsr223groovy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class PB_jrunscript2 {
    public static void main(String[] args) {
        try {
            ProcessBuilder pb = new ProcessBuilder();
            pb.command("jrunscript",
                    "-cp", "C:/groovy-4.0.6/lib/groovy-jsr223-4.0.6.jar;C:/groovy-4.0.6/lib/groovy-4.0.6.jar",
                    "-l","groovy",
                    "-f", "src/scripts/skrypt4.groovy",
                    "-encoding", "utf-8");
            pb.redirectError(ProcessBuilder.Redirect.INHERIT);
            Process p = pb.start();
            BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = r.readLine()) != null) {
                System.out.println("linia: "+ line);
            }
            int exitCode = p.waitFor();
            System.out.println("\nExited with code : " + exitCode);
            r.close();
            p.destroy();
        } catch (IOException | InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Po uruchomieniu klasy na konsoli zobaczymy:

linia: dividing(125, 3) = [41, 2]

Exited with code : 0
Skrypt skrypt4.groovy
package scripts

a = 125
b = 3

def dividing(int a, int b){
    def a1 = a.intdiv(b)
    def a2 = a % b
    def res = [a1, a2] as int[]
    return res;
}
result = dividing(a,b)
println("dividing($a, $b) = $result")
Klasa module-info.java
module jsr223groovy {

    requires org.apache.groovy;

    requires java.scripting;
    requires java.sql;

    exports jsr223groovy;
    opens jsr223groovy;
}