script

Access Java Object from a script

In this example we shall show you how to access a Java Object from a script. We are using the ScriptEngine interface that provides methods for the basic scripting functionality. To access a Java Object from a script one should perform the following steps:

  • Create a new ScriptEngineManager. The ScriptEngineManager implements a discovery and instantiation mechanism for ScriptEngine classes and also maintains a collection of key/value pairs storing state shared by all engines created by the Manager.
  • Use the getEngineByExtension(String extension) API method to look up and create a ScriptEngine for the js extension.
  • Use put(String key, Object value) API method of ScriptEngine to set a key/value pair in the state of the ScriptEngine that may either create a Java Language Binding to be used in the execution of scripts or be used in some other way, depending on whether the key is reserved. The value set here is a list of String car brands, with the key "brandArray".
  • Create a new String script to print the elements in the list.
  • Use the eval(String script) method to execute the script,
  • as described in the code snippet below.  

    01
    02
    03
    04
    05
    06
    07
    08
    09
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    package com.javacodegeeks.snippets.core;
     
    import javax.script.ScriptEngineManager;
    import javax.script.ScriptEngine;
    import javax.script.ScriptException;
      
    public class AccessJavaObjectFromScript {
        public static void main(String[] args) {
     
       
     
      // Create an array of car brands
     
      String[] brands = {"Audi", "Mercedes", "Renault", "Ford", "Seat"};
      
     
      // Script that reads the values of the array that contains string of car brands.
     
      String script =
     
     
        "var index; " +
     
     
        "var brands = brandsArray; " +
     
     
        " " +
     
     
        "for (index in brands) { " +
     
     
        "    println(brands[index]); " +
     
     
        "}" ;
      
     
      // Obtain a ScriptEngine instance.
     
      ScriptEngineManager manager = new ScriptEngineManager();
     
      ScriptEngine engine = manager.getEngineByExtension("js");
      
     
      // Place the brands array into the engine using brandsArray key.
     
      engine.put("brandsArray", brands);
      
     
      try {
     
     
    engine.eval(script);
     
      } catch (ScriptException e) {
     
     
    e.printStackTrace();
     
      }
        }
    }

    Output:

    Audi
    Mercedes
    Renault
    Ford
    Seat
    

      
    This was an example of how to access a Java Object from a script in Java.

    Do you want to know how to develop your skillset to become a Java Rockstar?
    Subscribe to our newsletter to start Rocking right now!
    To get you started we give you our best selling eBooks for FREE!
    1. JPA Mini Book
    2. JVM Troubleshooting Guide
    3. JUnit Tutorial for Unit Testing
    4. Java Annotations Tutorial
    5. Java Interview Questions
    6. Spring Interview Questions
    7. Android UI Design
    and many more ....
    I agree to the Terms and Privacy Policy

    Byron Kiourtzoglou

    Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
    Subscribe
    Notify of
    guest


    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    0 Comments
    Oldest
    Newest Most Voted
    Inline Feedbacks
    View all comments
    Back to top button