Modify Java object in script

With this example we are going to demonstrate how to modify a Java object in script. In short, to modify a Java object using script we have followed the steps below:

  • 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 names, with the key "brandList".
  • Create a new String script to print the list and then add new elements in the list.
  • Use the eval(String script) method to execute the script.
  • After executing the script print the list elements again. The list now has the new elements added to it by the script.

Let’s take a look at the code snippet that follows:  

package com.javacodegeeks.snippets.core;

import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.script.ScriptEngineManager;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
public class ModifyObjectFromScript {
	public static void main(String[] args) {

		// Create a List of car brands

  List<String> brands = new ArrayList<String>();






  // Obtain a ScriptEngine instance

  ScriptEngineManager manager = new ScriptEngineManager();

  ScriptEngine engine = manager.getEngineByExtension("js");

  // Set List into the engine

  engine.put("brandList", brands);

  try {


// Redisplay the modified version of brands list object.

for (String brand : brands) {

    System.out.println("Brand = " + brand);
  } catch (ScriptException e) {
    private static String getScript() {
    	// Script that reads and adds brands 
  String script =
    "var index; " +
    "var brands = brandList.toArray(); " +
    " " +
    "for (index in brands) { " +
    "    println(brands[index]); " +
    "}" +
    " " +
    "brandList.add("BMW"); " +
    "brandList.add("KIA"); " +
    "brandList.add("Smart"); ";
  return script;


Brand = Audi
Brand = Mercedes
Brand = Renault
Brand = Ford
Brand = Seat
Brand = BMW
Brand = KIA
Brand = Smart

This was an example of how to modify a Java object in script in Java.

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.
Notify of

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

Inline Feedbacks
View all comments
Back to top button