how to access methods from my class inside javascript in Nashorn

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-02 05:22:41

You must use the -classpath option of jrunscript or jjs.

-cp, -classpath (-cp path. Specify where to find user class files.)

The Java class:

package de.lhorn.so;

public class Foo {

    public final static int ZERO = 0;

    public static int i() {
        return 1;
    }
}

Compile it:

$ javac de/lhorn/so/Foo.java
$ tree de 
de
└── lhorn
    └── so
        ├── Foo.class
        └── Foo.java

Use it:

% jrunscript -cp .
nashorn> var Foo = Java.type("de.lhorn.so.Foo")
nashorn> Foo.ZERO
0
nashorn> Foo.i
[jdk.internal.dynalink.beans.SimpleDynamicMethod int de.lhorn.so.Foo.i()]
nashorn> Foo.i()
1
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!