title: Access object variable in Java Nashorn
body: <p>I have an object in my script, that contains fields and methods. I can call the methods in Java with <code>invokeMethod()</code> but can't seem to get the content of the fields of the object. I've got this JavaScript code:</p>
  
  <pre><code>var Test = { 
      TestVar: "SomeTest", 
  
      TestFunc: function() {
          print("Hello");
      }
  };
  </code></pre>
  
  <p>In this Java Class:</p>
  
  <pre><code>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();
          }
      }
  }
  </code></pre>
  
  <p>This gives me the output</p>
  
  <pre><code>[object Object]
  null
  null
  null
  Hello
  </code></pre>
  
  <p>Is there any way I can access the <code>TestVar</code> variable directly?</p>