Calling SageMath from Java

50 views
Skip to first unread message

meryem afendi

unread,
Oct 5, 2021, 12:15:38 PM10/5/21
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");
try 
         {
  
        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");
                writer.flush();
                writer.close();
                String line;
                while ((line = reader.readLine()) != null) {
                System.out.println(line);
                }
                p.waitFor();
                p.destroy();

    } 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

unread,
Oct 14, 2021, 8:10:24 AM10/14/21
to sage-devel
Hello,

(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 sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/2da50f7e-1bb6-4c4b-9dae-925683405d43n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages