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.sstatic.net/S0xg0.png

4
  • is it giving any error? can you try printing stderr? Commented May 2, 2016 at 18:13
  • It is not giving me any error. When I go to output.html, I am getting a 0(zero). Commented May 2, 2016 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() Commented May 2, 2016 at 23:46
  • Thanks, Michael. Will do that. Commented May 3, 2016 at 17:44

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

8 Comments

I edited my java program as you specified but still I am not getting any output. My output looks like this: i.sstatic.net/S0xg0.png
what happens when you run it on the command line? Do you see any output?
$ java -jar C:/Users/njaiswal/Desktop/Executable/Saxon.jar
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.
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`
|
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);
    })
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.