有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Nashorn在Java中运行带有导出功能的Javascript代码

可以用Nashorn在Java中运行包含导出函数的Javascript代码吗?我是纳肖恩的新手,所以我不确定js代码是否有限制。还有,如何将参数从Java传递到js代码

Javascript代码如下所示(取自here):

/** Given two circles (containing a x/y/radius attributes),
returns the intersecting points if possible.
note: doesn't handle cases where there are infinitely many
intersection points (circles are equivalent):, or only one intersection point*/
function circleCircleIntersection(p1, p2) {
    var d = distance(p1, p2),
        r1 = p1.radius,
        r2 = p2.radius;

    // if to far away, or self contained - can't be done
    if ((d >= (r1 + r2)) || (d <= Math.abs(r1 - r2))) {
        return [];
    }

    var a = (r1 * r1 - r2 * r2 + d * d) / (2 * d),
        h = Math.sqrt(r1 * r1 - a * a),
        x0 = p1.x + a * (p2.x - p1.x) / d,
        y0 = p1.y + a * (p2.y - p1.y) / d,
        rx = -(p2.y - p1.y) * (h / d),
        ry = -(p2.x - p1.x) * (h / d);

    return [{x: x0 + rx, y : y0 - ry },
            {x: x0 - rx, y : y0 + ry }];
}

/** Returns the center of a bunch of points */
function getCenter(points) {
    var center = {x: 0, y: 0};
    for (var i =0; i < points.length; ++i ) {
        center.x += points[i].x;
        center.y += points[i].y;
    }
    center.x /= points.length;
    center.y /= points.length;
    return center;
}

例如,我想通过使用以下代码提供多个点来调用js中的getCenter函数:

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    //      engine.eval("print('Hello World!');");
    engine.eval(new FileReader("circleintersection.js"));

    Invocable invocable = (Invocable) engine;

    Point a = new Point(3,2);
    Point b = new Point(5,3);
    Point c = new Point(1,4);
    Point d = new Point(2,5);
    Point e = new Point(6,6);

    Point[] points = {a,b,c,d,e};

    Point result = (Point) invocable.invokeFunction("getCenter", points);
    System.out.println(result.x);

但它给了我这样的错误

Exception in thread "main" java.lang.ClassCastException: jdk.nashorn.api.scripting.ScriptObjectMirror cannot be cast to Point

如何从js代码中获得结果


共 (1) 个答案

  1. # 1 楼答案

    Nashorn不完全支持ECMA6功能,导出是其中之一,不受支持

    参考资料:

    ECMA 6 support in Nashorn

    http://openjdk.java.net/jeps/292

    它开始支持JDK8本身的一些,以及JDK9中的一些。但我找不到支持出口的证据

    Also how do I pass the arguments from Java to the js code? If you have the following function

    var fun1 = function(name) {
        print('Hi there from Javascript, ' + name);
        return "greetings from javascript";
    };
    
    var fun2 = function (object) {
        print("JS Class Definition: " + Object.prototype.toString.call(object));
    };
    

    然后,你可以

    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval(new FileReader("script.js"));
    
    Invocable invocable = (Invocable) engine;
    
    Object result = invocable.invokeFunction("fun1", "Peter Parker");
    System.out.println(result);
    System.out.println(result.getClass());
    
    // Hi there from Javascript, Peter Parker
    // greetings from javascript
    // class java.lang.String
    

    这是取自https://winterbe.com/posts/2014/04/05/java8-nashorn-tutorial/