Αρχείο μηνός Σεπτέμβριος 2021

Retrieving ERROR messages from SWI-Prolog using Java

When executing prolog programs from java (either using the terminal or JPL) it is important to be able to find out if the prolog program has problems. Such programs may be uploaded by users, as in the case of Gorgias Cloud.

To get the output you need to execute your prolog file from the java runtime. Use the snippet below to get a demo:

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

public class TestCMD {

  public static void main(String[] args) {
    try {
      String userProjectPath = "Path to the folder of your file, e.g. E:\\";
      String userFilename = "Your file name, e.g. test.pl";
      Process p = Runtime.getRuntime()
          .exec("\"Path to swipl.exe, e.g. C:\\Program Files\\swipl\\bin\\swipl.exe\" -o /dev/null -c " + userProjectPath + userFilename);
      p.waitFor();
      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
      String line;
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
    } catch (IOException e1) {
      e1.printStackTrace();
    } catch (InterruptedException e2) {
      e2.printStackTrace();
    }
  }
}