2

I have created a child process in node.js and gave it an executable jar file location.

var fs = require('fs');

var exec = require('child_process').exec;
var child = exec('java -jar C:/Users/njaiswal/Desktop/Executable/Saxon.jar',
  function (error, stdout, stderr){
    fs.writeFile('output.html', +stdout);
    if(error !== null){
      console.log("Error -> "+error);
    }
});

module.exports = child;

This is my Saxon.java which I have converted it into jar file. As you can see, this java program takes result1.xml file and defaultfrontend.xslt stylesheet and convert it into an html web page (output.html).

import java.io.File;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Saxon {

  /**
   * Simple transformation method.
   * 
   * @param sourcePath
   *          - Absolute path to source xml file.
   * @param xsltPath
   *          - Absolute path to xslt file.
   * @param resultDir
   *          - Directory where you want to put resulting files.
   */
  public static void simpleTransform(String sourcePath, String xsltPath,
      String resultDir) {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    try {
      Transformer transformer = tFactory
          .newTransformer(new StreamSource(new File(xsltPath)));

      transformer.transform(new StreamSource(new File(sourcePath)),
          new StreamResult(new File(resultDir)));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  public static void main(String[] args) {
    // Set saxon as transformer.
    System.setProperty("javax.xml.transform.TransformerFactory",
        "net.sf.saxon.TransformerFactoryImpl");

    simpleTransform("result1.xml", "defaultfrontend.xslt", "output.html");

  }
}

So this program converts xml into html using xslt stylesheet. I want to get my output(in html) in node.js.

But when I try to run my javascript, I am not getting any output. The output looks like this: https://i.stack.imgur.com/S0xg0.png

Rose
  • 1,490
  • 5
  • 25
  • 56
  • is it giving any error? can you try printing stderr? – Deendayal Garg May 02 '16 at 18:13
  • It is not giving me any error. When I go to output.html, I am getting a 0(zero). – Rose May 02 '16 at 18:16
  • 1
    A minor comment about performance: initializing a Java VM to do a single transformation is a pretty high overhead. But there's no need to make it worse by using the JAXP loading method to create a TransformerFactory, when you already know that you want to use Saxon. Just instantiate Saxon directly by replacing `TransformerFactory.newInstance()` with `new net.sf.saxon.TransformerFactoryImpl()` – Michael Kay May 02 '16 at 23:46
  • Thanks, Michael. Will do that. – Rose May 03 '16 at 17:44

2 Answers2

1

It looks like you do not write anything to stdout but to a file. But in node you take the stdout and write it to the file again.

If you want java to output to stdout change

 transformer.transform(new StreamSource(new File(sourcePath)),
      new StreamResult(new File(resultDir)));

TO

transformer.transform(new StreamSource(new File(sourcePath)), 
     new StreamResult(new OutputStreamWriter(System.out, "UTF-8")));

source

Then you will be able to read stdout from nodejs

Community
  • 1
  • 1
oak
  • 2,898
  • 2
  • 32
  • 65
  • I edited my java program as you specified but still I am not getting any output. My output looks like this: http://i.stack.imgur.com/S0xg0.png – Rose May 02 '16 at 18:29
  • 1
    what happens when you run it on the `command line`? Do you see any output? – oak May 02 '16 at 18:43
  • 1
    $ java -jar C:/Users/njaiswal/Desktop/Executable/Saxon.jar – oak May 02 '16 at 18:43
  • When I ran "java -jar C:/Users/njaiswal/Desktop/Executable/Saxon.jar" it shows "Caused by: java.io.FileNotFoundException: C:\demo\6_javanode\onebox-default.xsl (The system cannot find the file specified)". Actually my defaultfrontend stylesheet is using few other files like onebox-default.xsl. Actually I have used eclipse to make a executable jar file. The java program is using xml and xslt files. This might be the error because the xml and xslt file are in my eclipse. I think the jar file is not using my xml and xslt files. – Rose May 02 '16 at 18:52
  • 1
    Well according to your code, the `xsl` and the `html` have to be on the same folder as your `running location` i.e by your last run at `C:\demo\6_javanode\` – oak May 02 '16 at 18:57
  • Yes. Got it. Now when I run " java -jar C:/Users/njaiswal/Desktop/Executable/Saxon.jar ", I am getting my html web page. But when I run through node.js, I am getting just "NaN" in output.html. – Rose May 02 '16 at 19:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110853/discussion-between-nupur-jaiswal-and-oak). – Rose May 02 '16 at 19:03
  • Sure. Thanks. Appreciate your time – Rose May 02 '16 at 19:09
1

This worked for me:

var fs = require('fs');
var pathToFile = 'output.html';

var exec = require('child_process').exec;
var child = exec('java -jar Saxon.jar', function (error, stdout, stderr){
    fs.writeFile(pathToFile, stdout, function(err) {
       if(err) console.error(err);
    })
});
Rose
  • 1,490
  • 5
  • 25
  • 56