ScriptEngine como passar uma string que representa JSON?

Eu estava tentando passar um objeto String que representa uma string JSON de java para JavaScript usando ScriptEngine no jdk 1.6.

Meu código funciona no JSFIDDLEhttp://jsfiddle.net/hn4yk/13/ mas quando eu tento carregá-lo com o ScriptEngine ele não funciona e lança uma exceção sun.org.mozilla.javascript.internal.EcmaError: TypeError: Não é possível ler a propriedade "resultList" de undefined (# 1164)

Eu testei que a variável é passada corretamente, mas o método eval ou JSON.parse não funciona! Eu tentei colocar a biblioteca para JSON.parse e minha função no mesmo arquivo!

Este é o meu código java:

public class InvokeScriptMethod {


    public static void main(String[] args) throws Exception {

        InputStream stream = new FileInputStream("C:/Subclipse/TestJavascriptServerSide/jsonInput.txt");
        StringWriter writer = new StringWriter();
        IOUtils.copy(stream, writer, "UTF-8");
        String jsonString = writer.toString();

        InputStream javascriptStream = new FileInputStream("C:/Subclipse/TestJavascriptServerSide/Script");
        StringWriter writerScript = new StringWriter();
        IOUtils.copy(javascriptStream, writerScript, "UTF-8");
        String javascriptString = writerScript.toString();

        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByExtension("js");

        engine.eval(javascriptString);

        Invocable inv = (Invocable) engine;

        String convertedJson = (String)inv.invokeFunction("convertJSON",jsonString);

        System.out.println("results"+convertedJson);

        // now should I convert it to a writer to reply in a webapplication as response.getWriter().print(convertedJson);
        // and I would like to don't lose the JSON formatting to see data with indentation
    }
}

Qualquer ajuda é apreciada! Obrigado!

questionAnswers(1)

yourAnswerToTheQuestion