jetma / jav8

Automatically exported from code.google.com/p/jav8
0 stars 0 forks source link

The "Hello, world!" example is not working. #6

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I copy the example you have on the home page and set the library 
jav8-jsr223-linux-i386-0.3.jar. 
When I run the the program.

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class test
{
    public static void main(String[] arg)
    {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("jav8");

        try {
            engine.eval("print('Hello, world!')");
        } catch (ScriptException ex) {
            ex.printStackTrace();
        }
    }
}

I got the next exception

Exception in thread "main" java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:247)
    at sun.misc.Service$LazyIterator.next(Service.java:271)
    at javax.script.ScriptEngineManager.initEngines(ScriptEngineManager.java:109)
    at javax.script.ScriptEngineManager.access$000(ScriptEngineManager.java:37)
    at javax.script.ScriptEngineManager$1.run(ScriptEngineManager.java:80)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.script.ScriptEngineManager.init(ScriptEngineManager.java:78)
    at javax.script.ScriptEngineManager.<init>(ScriptEngineManager.java:51)
    at test.main(test.java:9)
Caused by: java.lang.NullPointerException
    at lu.flier.script.V8ScriptEngineFactory.loadLibrary(Unknown Source)
    at lu.flier.script.V8ScriptEngineFactory.<clinit>(Unknown Source)
    ... 10 more

Do I missing something?
Thanks for your help.

Original issue reported on code.google.com by gustavo....@modernizingmedicine.com on 5 Apr 2012 at 1:55

GoogleCodeExporter commented 9 years ago
yes, i can't do it work. who can help us?

Original comment by blackbla...@126.com on 5 Jul 2012 at 2:20

GoogleCodeExporter commented 9 years ago
The issue here is that there is no 'print' function exposed to the javascript 
interface.

You are of-course at liberty to define your own. The following worked for me...

====
Test.java:
----
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class Test {

  public static void main(String[] args) throws ScriptException {
    ScriptEngine eng = new ScriptEngineManager().getEngineByName("jav8"); 
    Jav8Printer jav8 = new Jav8Printer();    
    eng.put("jav8", jav8);
    eng.eval("var print = function (arg) {jav8.print(arg);};");

    eng.eval("print('Hello, world!');");
  }
}
====

====
Jav8Printer.java
----
public class Jav8Printer {

   public void print(Object args) {
      System.out.println(args.toString());
   }
}
====

What does not work however is saying:
    eng.eval("var print = jav8.print;");
and I shall file a separate issue report against that.

Original comment by a.freddi...@gmail.com on 22 Aug 2012 at 8:36

GoogleCodeExporter commented 9 years ago
i have same issue on window, debug find that eng == null. 
what can i do?

Original comment by blackbla...@126.com on 19 Sep 2012 at 4:03

GoogleCodeExporter commented 9 years ago
When "eng == null" this likely means the jar has not been installed as a 
referenced library to your project.

Original comment by MUDC...@gmail.com on 11 Feb 2014 at 11:51