0

I'm using the Saxon9 old version to use the java runtime exec to execute a ruby script, this is the first time I'm trying out a runtime exec. I used the below code to run a ruby script it's not producing any results to value.

Due to a limitation, I'm unable to use the SaxonEE or Saxon PE so I'm trying to use the old version.

<xsl:variable name="javatest" select="runtime:exec(runtime:getRuntime(), 'ruby test.rb')" />
<xsl:variable name="waiting" select ="process:waitFor($javatest)" />
<xsl:value-of select="$javatest" />

The above code is not producing an actual result from the ruby script, It always generates results like java.lang.ProcessImpl@170c109. Is this achievable in saxon9?

Jason
  • 65
  • 7
  • That seems like the Java code is being executed, otherwise you wouldn't get a `java.lang.ProcessImp` instance. So be more precise, which version of Saxon 9 exactly do you use, what is a minimal but complete stylesheet, what is the result you expect? Are you sure you have the right Java API calls to have the result of the ruby script execution returned? Otherwise add the tag for Java as well. – Martin Honnen Jul 28 '22 at 08:47
  • I suppose you need/want to get the output stream of the process and read it: https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Process.html#getOutputStream(). There might be more suitable, higher level APIs to do that in a single call, I would guess. – Martin Honnen Jul 28 '22 at 08:56
  • I tried it with "Saxon 9.1.0.8", and the ruby script will prints a string – Jason Jul 28 '22 at 09:08
  • I think 9.1 is fine to run embedded/inline Java code via reflexion but of course you need to right Java code. The code you have will (probably) write the output of the process running the ruby script somewhere but the `exec` method does not return that output, it returns the process handle from which you need to access the output stream and read it. Doing that is purely a Java question and has nothing to do with XSLT. – Martin Honnen Jul 28 '22 at 09:11
  • https://stackoverflow.com/a/20624914/252228 might help with the Java code. – Martin Honnen Jul 28 '22 at 09:23

1 Answers1

0

Firstly, the value of variable $javatest is going to be a wrapper object around the result of calling java.lang.RunTime.exec(), which is an instance of java.lang.Process, and that's exactly what you're seeing. If you want to get the output of that process, you probably need to call its getOutputStream() method. It's probably all going to be easier if you write a utility method in Java that calls the exec() method, gets the output stream, and returns the contents of the output stream as a string.

Second point: the variable $waiting is declared but never used, so it's never going to be evaluated. Don't try to call external methods like this for the sake of their side-effects. Best to include the waitFor() call in your Java utility method; but if you want to do it all from XSLT, use the integer result of the method somehow, for example do

<xsl:if test="$waiting ne 0">
  <xsl:message terminate="yes">External process failed</xsl:message>
</xsl:if>
Michael Kay
  • 156,231
  • 11
  • 92
  • 164