How to instantiate a Java class in JavaScript using Nashorn?

非 Y 不嫁゛ 提交于 2019-12-13 05:18:58

问题


Java Code

package org.something;

public class myClass{
    static public Double getAvg(){
       //Returns an average
    }
}

JavaScript Code

var aJavaClass = Java.type('org.something.myClass');
var avg = aJavaClass.getAvg();

This is pretty much what I am attempting to do. The application I'm creating requires the Java portion to transfer a Double to JavaScript for use in an embedded browser. I'd looked into Nashorn and several tutorials for it, but while their code is fine, mine fails to run correctly whenever aJavaClass is declared. Without this line, everything works.

To this point, the only thing I can think of is there being an import issue, but I'm not certain on how to test that.

Is there any reason why JS would not be recognizing my class?

来源:https://stackoverflow.com/questions/24965194/how-to-instantiate-a-java-class-in-javascript-using-nashorn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!