3

I have a java program that opens, reads, and write multiple files. it also contains complex logic formatting.

Now I wrote on jsfiddle an easy javascript here to do some tree traversals and parsing for me, and it is much easier than implement in Java.

My challenge now is how can i “embed” this Javascript script into my Java method. I’m primarily a Java coder.

The pseudocode for the Java method is something like this:

<Java method begins……>

      String input = “ABC”  //its more complex than ABC
      String o1= null;
         //JavaScript script begins,
             //Javascript evaluates the Java string input
             //Javascript output is assigned to Java o1
               o1 =  output;
        //Javascript script ends 

    //maniputate and process Java string o1 - which is not null
<Java method ends>
bouncingHippo
  • 5,940
  • 21
  • 67
  • 107
  • http://stackoverflow.com/questions/7487908/how-can-i-use-javascript-in-java – JamesB Jun 26 '14 at 15:38
  • 1
    Scripting Engine introduced in Java 6: http://docs.oracle.com/javase/6/docs/api/javax/script/package-summary.html – JamesB Jun 26 '14 at 15:44

3 Answers3

2

Fairly simple to do with Java 8. You can use Nashorn.

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class JsTest {

  @org.junit.Test
  public void test() throws Exception  {
    ScriptEngine jsEngine = new ScriptEngineManager().getEngineByName("nashorn");
    jsEngine.eval("var say = function(name) {return 'Hello ' + name;}");

    Invocable jsScript = (Invocable) jsEngine;

    Object result = jsScript.invokeFunction("say", "XYZ");
    System.out.println(result);
  }
}

The eval method has various possible parameters. It would also be possible, loading the script from a file.

A good tutorial can be found here: http://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial

spa
  • 5,059
  • 1
  • 35
  • 59
  • i get a null pointer error when i copy and paste your code. It fails at `jsEngine.eval("var say = function(name) {return 'Hello ' + name;}");` – bouncingHippo Jun 26 '14 at 15:55
  • Do you have JDK8? Works flawlessly on my machine without any external deps. I would assume that jsEngine is null as "nashorn" is not known in old JDKs. – spa Jun 26 '14 at 16:00
  • ahhh faux pas you're right, i'm using Java 6. I'll try to install Java 8 and hope it doesn't break my environemnt – bouncingHippo Jun 26 '14 at 16:03
  • 1
    There are no updates for JDK 6 since Feb 2013. I think you should upgrade :-) – spa Jun 27 '14 at 15:39
  • thanks for the handholding thus far, i appreciate it! +1 for code snippet too! :) – bouncingHippo Jun 27 '14 at 18:35
1

You can do one of two things.

  1. Translate the code into Java. Quite simple, depending on code size. Mainly you need to change vars to typed variables.

  2. Use a Javascript engine like Rhino, into which you can feed your Javascript and it'll get the output.

PurkkaKoodari
  • 6,703
  • 6
  • 37
  • 58
  • [And here](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Embedding_tutorial#usingJSObjs)'s the specific Rhino doc page – blgt Jun 26 '14 at 15:42
  • not sure if it helps, but here's the javascript in question http://jsfiddle.net/DXs8p/47/ – bouncingHippo Jun 26 '14 at 15:43
0

Personally, I would avoid embedding JavaScript into your Java code for this purpose and do it using standard Java. There are various String processing libraries you can use for this that are already built into Java.

I would be very surprised if what you're trying to do isn't doable in Java using String operators and regular expressions.

I would also imagine that there would be a pretty big performance hit to passing off a String to JavaScript for processing and then passing back to Java. It would make way more sense, and be more efficient, to do it all in Java.

Not sure exactly how you'd embed JavaScript in your Java, but there are JS libraries that may be worth looking at (this link to Nashorn (a bit like Rhino) may help). I would avoid using this unless absolutely necessary though.

Malcolm Murray
  • 323
  • 2
  • 10