Calling SageMath from Java

Skip to first unread message

meryem afendi

Oct 5, 2021, 12:15:38 PMOct 5
to sage-devel
Hello everyone,
I tried to call SageMath from a java program using the Class ProcessBuilder. This code success to launch SageMath but when i want to use the InputStream to interact with Sage (For example a simple code 1+2) it does not work. iSageMath runs without sending the result of 1+2. 
Thank you.

 ProcessBuilder pb = new ProcessBuilder("C:\\Users\\Meryem\\AppData\\Local\\SageMath 9.2\\runtime\\bin\\mintty.exe", "-t","SageMath 9.2 Console", "-i", "sagemath.ico", "/bin/bash", "--login", "-c",  "/opt/sagemath-9.2/sage");
        Process p = pb.start();
        OutputStream stdin = p.getOutputStream();
                InputStream stdout = p.getInputStream();
              //InputStream stderr = p.getErrorStream();

                BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

                writer.write("1+2" + "\n");
                String line;
                while ((line = reader.readLine()) != null) {

    } catch (IOException e) {
        System.out.println("I/O error:" + e.getMessage());
    } catch (Exception e) {
        System.out.println("Error:" + e.getMessage());
return null;

E. Madison Bray

Oct 14, 2021, 8:10:24 AMOct 14
to sage-devel

(For anyone confused about this question, it's about the Windows
release of Sage).

The command you're trying to run is not going to work. I can see you
probably copied it from the desktop shortcut that launches Sage in a
terminal Window. mintty.exe is the terminal emulator that comes with
Cygwin and that's used in the Windows version of Sage to provide a
UNIX-like shell terminal. It has nothing to do with Sage itself which
runs in a Python interpreter. If you want to control a Sage
interpreter as an interactive process, you only need to run the Python
interpreter directly, possibly with some appropriate environment
variables set.

However I must ask, what is it you are *actually* trying to build?
Wrapping an interactive session with a Python interpreter is likely
not the most effective way to get data in, and especially out.
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
> To view this discussion on the web visit
Reply all
Reply to author
0 new messages