hi iam trying to execute maple using a servlet and iam getting a error can anyone help me. iam trying to make maple web enabled. can anyone help me know how to call other programs from java.... i have my sample file that gives a error.
import java.io.*; import java.util.*;
public class Maple { private Process process; // Used to call Maple. private OutputStream maple_input; private BufferedReader maple_output; public Maple() throws IOException { this.process = Runtime.getRuntime().exec("cmaple -q"); // <-- change cmaple to maple if running under unix. this.maple_input = process.getOutputStream(); this.maple_output = new BufferedReader(new InputStreamReader(process.getInputStream())); }
// Close the Maple session: public void close() throws IOException { String quit = "quit\n"; maple_input.write(quit.getBytes()); maple_input.flush(); try{ process.waitFor(); } catch(InterruptedException ex) {} }
// Execute a given command 'cmd' and write the results to 'out'. public synchronized void exec(String cmd, PrintWriter out) throws IOException { // execute user's command: String Cmd = cmd + ";\n"; maple_input.write( Cmd.getBytes() ); maple_input.flush();
// Log the command: System.out.println("> "+ cmd + ";");
// To detect the end of Maple's user output, pass the follwing //'unique' command to Maple: String terminator_cmd = "JAVA:END OF A MAPLE COMMAND IS HERE"; maple_input.write( ("printf(`" + terminator_cmd +"\\n`):\n").getBytes() ); maple_input.flush();
// Next, read the output, line-by-line, until terminator_cmd is read: String s; while(true) { s = maple_output.readLine(); if (s.indexOf(terminator_cmd) != -1 ) { break; } if (out != null) { out.println(s); } } } }
Maple maple = new Maple(); maple.exec("2+2", out); maple.close();