Title: Modify Java object in script - Java Code Geeks
Open Graph Title: Modify Java object in script - Java Code Geeks
Description: 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
Open Graph Description: 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
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/"},"author":{"name":"Byron Kiourtzoglou","@id":"https://examples.javacodegeeks.com/#/schema/person/3b111ec1048740c68c9e709ff6240015"},"headline":"Modify Java object in script","datePublished":"2012-11-11T19:48:06+00:00","dateModified":"2013-10-02T18:07:40+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/"},"wordCount":220,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","keywords":["core java","script"],"articleSection":["script"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/","url":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/","name":"Modify Java object in script - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2012-11-11T19:48:06+00:00","dateModified":"2013-10-02T18:07:40+00:00","description":"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","breadcrumb":{"@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/java-development/core-java/script/modify-java-object-in-script/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"script","item":"https://examples.javacodegeeks.com/category/java-development/core-java/script/"},{"@type":"ListItem","position":5,"name":"Modify Java object in script"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/3b111ec1048740c68c9e709ff6240015","name":"Byron Kiourtzoglou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Byron-Kiourtzoglou-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/10/Byron-Kiourtzoglou-96x96.jpg","caption":"Byron Kiourtzoglou"},"description":"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.","sameAs":["https://www.pivotalgamers.com/","https://www.linkedin.com/in/byron-kiourtzoglou-530ab222"],"url":"https://examples.javacodegeeks.com/author/byron-kiourtzoglou/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"script","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/script\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2012-11-11T19:48:06+02:00","datePublished":"2012-11-11T19:48:06+02:00","dateModified":"2013-10-02T21:07:40+03:00","headline":"Modify Java object in script","name":"Modify Java object in script","keywords":"core java,script","url":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/script\/modify-java-object-in-script\/","description":"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\u00a0we have followed the steps below:Create a new ScriptEngineManager. T","copyrightYear":"2012","articleSection":"script","articleBody":"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\u00a0we 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\u2019s take a look at the code snippet that follows: \u00a0\r\n\r\npackage com.javacodegeeks.snippets.core;\r\n\r\nimport javax.script.ScriptEngineManager;\r\nimport javax.script.ScriptEngine;\r\nimport javax.script.ScriptException;\r\n \r\nimport javax.script.ScriptEngineManager;\r\nimport javax.script.ScriptEngine;\r\nimport javax.script.ScriptException;\r\nimport java.util.Date;\r\nimport java.util.List;\r\nimport java.util.ArrayList;\r\n \r\npublic class ModifyObjectFromScript {\r\n \r\n\tpublic static void main(String[] args) {\r\n\r\n \r\n\t\t\/\/ Create a List of car brands\r\n\r\n List<String> brands = new ArrayList<String>();\r\n\r\n brands.add(\"Audi\");\r\n\r\n brands.add(\"Mercedes\");\r\n\r\n brands.add(\"Renault\");\r\n\r\n brands.add(\"Ford\");\r\n\r\n brands.add(\"Seat\");\r\n\r\n \/\/ Obtain a ScriptEngine instance\r\n\r\n ScriptEngineManager manager = new ScriptEngineManager();\r\n\r\n ScriptEngine engine = manager.getEngineByExtension(\"js\");\r\n \r\n\r\n \/\/ Set List into the engine\r\n\r\n engine.put(\"brandList\", brands);\r\n\r\n try {\r\n\r\n\r\nengine.eval(getScript());\r\n\r\n\/\/ Redisplay the modified version of brands list object.\r\n\r\nfor (String brand : brands) {\r\n\r\n System.out.println(\"Brand = \" + brand);\r\n}\r\n } catch (ScriptException e) {\r\ne.printStackTrace();\r\n }\r\n }\r\n \r\n private static String getScript() {\r\n \t\/\/ Script that reads and adds brands \r\n String script =\r\n \"var index; \" +\r\n \"var brands = brandList.toArray(); \" +\r\n \" \" +\r\n \"for (index in brands) { \" +\r\n \" println(brands[index]); \" +\r\n \"}\" +\r\n \" \" +\r\n \"brandList.add(\"BMW\"); \" +\r\n \"brandList.add(\"KIA\"); \" +\r\n \"brandList.add(\"Smart\"); \";\r\n return script;\r\n }\r\n}\r\n\r\n\r\nOutput:\r\n\r\nAudi\r\nMercedes\r\nRenault\r\nFord\r\nSeat\r\nBrand = Audi\r\nBrand = Mercedes\r\nBrand = Renault\r\nBrand = Ford\r\nBrand = Seat\r\nBrand = BMW\r\nBrand = KIA\r\nBrand = Smart\r\n\r\n \u00a0\r\nThis was an example of how to modify a Java object in script\u00a0in Java.","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/java-development\/core-java\/script\/modify-java-object-in-script\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Byron Kiourtzoglou","url":"https:\/\/examples.javacodegeeks.com\/author\/byron-kiourtzoglou\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2012-11-11T19:48:06+00:00 |
| article:modified_time | 2013-10-02T18:07:40+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Byron Kiourtzoglou |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Byron Kiourtzoglou |
| twitter:label2 | Est. reading time |
| twitter:data2 | 2 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1