最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Access object variable in Java Nashorn - Stack Overflow

matteradmin5PV0评论

I have an object in my script, that contains fields and methods. I can call the methods in Java with invokeMethod() but can't seem to get the content of the fields of the object. I've got this JavaScript code:

var Test = { 
    TestVar: "SomeTest", 

    TestFunc: function() {
        print("Hello");
    }
};

In this Java Class:

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

public class ScriptTest {
    public static void main(String[] args) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");

        try {
            engine.eval("var Test = { TestVar: \"SomeTest\", TestFunc: function() { print(\"Hello\");}};");
        } catch (ScriptException e) {
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println(engine.get("Test"));
        System.out.println(engine.get("Test.TestVar"));
        System.out.println(engine.get("Test[TestVar]"));
        System.out.println(engine.get("Test[\"TestVar\"]"));

        Invocable inv = (Invocable) engine;

        try {
            inv.invokeMethod(engine.get("Test"), "TestFunc");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

This gives me the output

[object Object]
null
null
null
Hello

Is there any way I can access the TestVar variable directly?

I have an object in my script, that contains fields and methods. I can call the methods in Java with invokeMethod() but can't seem to get the content of the fields of the object. I've got this JavaScript code:

var Test = { 
    TestVar: "SomeTest", 

    TestFunc: function() {
        print("Hello");
    }
};

In this Java Class:

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

public class ScriptTest {
    public static void main(String[] args) {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");

        try {
            engine.eval("var Test = { TestVar: \"SomeTest\", TestFunc: function() { print(\"Hello\");}};");
        } catch (ScriptException e) {
            e.printStackTrace();
            System.exit(1);
        }

        System.out.println(engine.get("Test"));
        System.out.println(engine.get("Test.TestVar"));
        System.out.println(engine.get("Test[TestVar]"));
        System.out.println(engine.get("Test[\"TestVar\"]"));

        Invocable inv = (Invocable) engine;

        try {
            inv.invokeMethod(engine.get("Test"), "TestFunc");
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }
}

This gives me the output

[object Object]
null
null
null
Hello

Is there any way I can access the TestVar variable directly?

Share Improve this question asked May 19, 2015 at 14:47 GeminusGeminus 1561 silver badge9 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Either:

engine.eval("Test.TestVar");

or

((JSObject)engine.get("Test")).getMember("TestVar");

should work.

Post a comment

comment list (0)

  1. No comments so far