<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="it">
	<id>http://wiki.netsitech.com/index.php?action=history&amp;feed=atom&amp;title=Programmazione%3AJava%2FJava-Scripting</id>
	<title>Programmazione:Java/Java-Scripting - Cronologia</title>
	<link rel="self" type="application/atom+xml" href="http://wiki.netsitech.com/index.php?action=history&amp;feed=atom&amp;title=Programmazione%3AJava%2FJava-Scripting"/>
	<link rel="alternate" type="text/html" href="http://wiki.netsitech.com/index.php?title=Programmazione:Java/Java-Scripting&amp;action=history"/>
	<updated>2026-05-04T20:05:14Z</updated>
	<subtitle>Cronologia della pagina su questo sito</subtitle>
	<generator>MediaWiki 1.34.4</generator>
	<entry>
		<id>http://wiki.netsitech.com/index.php?title=Programmazione:Java/Java-Scripting&amp;diff=9281&amp;oldid=prev</id>
		<title>Mazzotti: /* &quot;Hello, World&quot; */</title>
		<link rel="alternate" type="text/html" href="http://wiki.netsitech.com/index.php?title=Programmazione:Java/Java-Scripting&amp;diff=9281&amp;oldid=prev"/>
		<updated>2016-02-23T08:36:54Z</updated>

		<summary type="html">&lt;p&gt;&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;&amp;quot;Hello, World&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;it&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Versione meno recente&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Versione delle 08:36, 23 feb 2016&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l22&quot; &gt;Riga 22:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Riga 22:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;From the ScriptEngineManager instance, we request a JavaScript engine instance using getEngineByName method. On the script engine, the eval method is called to execute a given String as JavaScript code! For brevity, in this as well as in subsequent examples, we have not shown exception handling. There are checked and runtime exceptions thrown from javax.script API. Needless to say, you have to handle the exceptions appropriately.  &lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;From the ScriptEngineManager instance, we request a JavaScript engine instance using getEngineByName method. On the script engine, the eval method is called to execute a given String as JavaScript code! For brevity, in this as well as in subsequent examples, we have not shown exception handling. There are checked and runtime exceptions thrown from javax.script API. Needless to say, you have to handle the exceptions appropriately.  &lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;code &lt;/del&gt;java&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;syntaxhighlight lang=&amp;quot;&lt;/ins&gt;java&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;&amp;quot;&lt;/ins&gt;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;import javax.script.*;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;import javax.script.*;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;public class EvalScript {&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;public class EvalScript {&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l34&quot; &gt;Riga 34:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Riga 34:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     }&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;}&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;}&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;code&lt;/del&gt;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/&lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;syntaxhighlight&lt;/ins&gt;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Evaluating a Script File===&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;===Evaluating a Script File===&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Mazzotti</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.netsitech.com/index.php?title=Programmazione:Java/Java-Scripting&amp;diff=2777&amp;oldid=prev</id>
		<title>Rimondini il 08:30, 14 apr 2008</title>
		<link rel="alternate" type="text/html" href="http://wiki.netsitech.com/index.php?title=Programmazione:Java/Java-Scripting&amp;diff=2777&amp;oldid=prev"/>
		<updated>2008-04-14T08:30:06Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;it&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Versione meno recente&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Versione delle 08:30, 14 apr 2008&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l1&quot; &gt;Riga 1:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Riga 1:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;[[Programmazione:Java|&amp;lt;&amp;lt; Back to Java]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt; &lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=Java Scripting=&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;=Java Scripting=&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Who is the Java Scripting API For? ==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt; &lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Who is the Java Scripting API For? ==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Rimondini</name></author>
		
	</entry>
	<entry>
		<id>http://wiki.netsitech.com/index.php?title=Programmazione:Java/Java-Scripting&amp;diff=2155&amp;oldid=prev</id>
		<title>Rimondini il 16:05, 13 nov 2007</title>
		<link rel="alternate" type="text/html" href="http://wiki.netsitech.com/index.php?title=Programmazione:Java/Java-Scripting&amp;diff=2155&amp;oldid=prev"/>
		<updated>2007-11-13T16:05:41Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Nuova pagina&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Java Scripting=&lt;br /&gt;
==Who is the Java Scripting API For? ==&lt;br /&gt;
Some useful characteristics of scripting languages are: &lt;br /&gt;
Convenience: Most scripting languages are dynamically typed. You can usually create new variables without declaring the variable type, and you can reuse variables to store objects of different types. Also, scripting languages tend to perform many type conversions automatically, for example, converting the number 10 to the text &amp;quot;10&amp;quot; as necessary. &lt;br /&gt;
Developing rapid prototypes: You can avoid the edit-compile-run cycle and just use edit-run! &lt;br /&gt;
Application extension/customization: You can &amp;quot;externalize&amp;quot; parts of your application - like configuration scripts, business logic/rules and math expressions for financial applications. &lt;br /&gt;
&amp;quot;Command line&amp;quot; shells for applications -for debugging, runtime/deploy time configuration etc. Most applications have a web-based GUI configuaration tool these days. But sysadmins/deployers frequently prefer command line tools. Instead of inventing ad-hoc scripting language for that purpose, a &amp;quot;standard&amp;quot; scripting language can be used. &lt;br /&gt;
The JavaTM Scripting API is a scripting language indepedent framework for using script engines from Java code. With the Java Scripting API, it is possible to write customizable/extendable applications in the Java language and leave the customization scripting language choice to the end user. The Java application developer need not choose the extension language during development. If you write your application with JSR-223 API, then your users can use any JSR-223 compliant scripting language. &lt;br /&gt;
&lt;br /&gt;
==Scripting Package==&lt;br /&gt;
The Java Scripting functionality is in the javax.script package. This is a relatively small, simple API. The starting point of the scripting API is the ScriptEngineManager class. A ScriptEngineManager object can discover script engines through the jar file service discovery mechanism. It can also instantiate ScriptEngine objects that interpret scripts written in a specific scripting language. The simplest way to use the scripting API is as follows: &lt;br /&gt;
&lt;br /&gt;
Create a ScriptEngineManager object. &lt;br /&gt;
Get a ScriptEngine object from the manager. &lt;br /&gt;
Evaluate script using the ScriptEngine's eval methods. &lt;br /&gt;
Now, it is time to look at some sample code. While it is not mandatory, it may be useful to know a bit of JavaScript to read these examples. &lt;br /&gt;
&lt;br /&gt;
==Examples==&lt;br /&gt;
===&amp;quot;Hello, World&amp;quot;===&lt;br /&gt;
From the ScriptEngineManager instance, we request a JavaScript engine instance using getEngineByName method. On the script engine, the eval method is called to execute a given String as JavaScript code! For brevity, in this as well as in subsequent examples, we have not shown exception handling. There are checked and runtime exceptions thrown from javax.script API. Needless to say, you have to handle the exceptions appropriately. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code java&amp;gt;&lt;br /&gt;
import javax.script.*;&lt;br /&gt;
public class EvalScript {&lt;br /&gt;
    public static void main(String[] args) throws Exception {&lt;br /&gt;
        // create a script engine manager&lt;br /&gt;
        ScriptEngineManager factory = new ScriptEngineManager();&lt;br /&gt;
        // create a JavaScript engine&lt;br /&gt;
        ScriptEngine engine = factory.getEngineByName(&amp;quot;JavaScript&amp;quot;);&lt;br /&gt;
        // evaluate JavaScript code from String&lt;br /&gt;
        engine.eval(&amp;quot;print('Hello, World')&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Evaluating a Script File===&lt;br /&gt;
In this example, we call the eval method that accepts java.io.Reader for the input source. The script read by the given reader is executed. This way it is possible to execute scripts from files, URLs and resources by wrapping the relevant input stream objects as readers. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code java&amp;gt;&lt;br /&gt;
import javax.script.*;&lt;br /&gt;
public class EvalFile {&lt;br /&gt;
    public static void main(String[] args) throws Exception {&lt;br /&gt;
        // create a script engine manager&lt;br /&gt;
        ScriptEngineManager factory = new ScriptEngineManager();&lt;br /&gt;
        // create JavaScript engine&lt;br /&gt;
        ScriptEngine engine = factory.getEngineByName(&amp;quot;JavaScript&amp;quot;);&lt;br /&gt;
        // evaluate JavaScript code from given file - specified by first argument&lt;br /&gt;
        engine.eval(new java.io.FileReader(args[0]));&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
Let us assume that we have the file named &amp;quot;test.js&amp;quot; with the following text: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
println(&amp;quot;This is hello from test.js&amp;quot;);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
We can run the above Java as &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;&lt;br /&gt;
java EvalFile test.js&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Script Variables===&lt;br /&gt;
When you embed script engines and scripts with your Java application, you may want to expose your application objects as global variables to scripts. This example demonstrates how you can expose your application objects as global variables to a script. We create a java.io.File in the application and expose the same as a global variable with the name &amp;quot;file&amp;quot;. The script can access the variable - for example, it can call public methods on it. Note that the syntax to access Java objects, methods and fields is dependent on the scripting language. JavaScript supports the most &amp;quot;natural&amp;quot; Java-like syntax. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code java&amp;gt;&lt;br /&gt;
public class ScriptVars { &lt;br /&gt;
    public static void main(String[] args) throws Exception {&lt;br /&gt;
        ScriptEngineManager manager = new ScriptEngineManager();&lt;br /&gt;
        ScriptEngine engine = manager.getEngineByName(&amp;quot;JavaScript&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        File f = new File(&amp;quot;test.txt&amp;quot;);&lt;br /&gt;
        // expose File object as variable to script&lt;br /&gt;
        engine.put(&amp;quot;file&amp;quot;, f);&lt;br /&gt;
&lt;br /&gt;
        // evaluate a script string. The script accesses &amp;quot;file&amp;quot; &lt;br /&gt;
        // variable and calls method on it&lt;br /&gt;
        engine.eval(&amp;quot;print(file.getAbsolutePath())&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Invoking Script Functions and Methods===&lt;br /&gt;
Sometimes you may want to call a specific scripting function repeatedly - for example, your application menu functionality might be implemented by a script. In your menu's action event handler you may want to call a specific script function. The following example demonstrates invoking a specific script function from Java code. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code java&amp;gt;&lt;br /&gt;
import javax.script.*;&lt;br /&gt;
&lt;br /&gt;
public class InvokeScriptFunction {&lt;br /&gt;
    public static void main(String[] args) throws Exception {&lt;br /&gt;
        ScriptEngineManager manager = new ScriptEngineManager();&lt;br /&gt;
        ScriptEngine engine = manager.getEngineByName(&amp;quot;JavaScript&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        // JavaScript code in a String&lt;br /&gt;
        String script = &amp;quot;function hello(name) { print('Hello, ' + name); }&amp;quot;;&lt;br /&gt;
        // evaluate script&lt;br /&gt;
        engine.eval(script);&lt;br /&gt;
&lt;br /&gt;
        // javax.script.Invocable is an optional interface.&lt;br /&gt;
        // Check whether your script engine implements or not!&lt;br /&gt;
        // Note that the JavaScript engine implements Invocable interface.&lt;br /&gt;
        Invocable inv = (Invocable) engine;&lt;br /&gt;
&lt;br /&gt;
        // invoke the global function named &amp;quot;hello&amp;quot;&lt;br /&gt;
        inv.invokeFunction(&amp;quot;hello&amp;quot;, &amp;quot;Scripting!!&amp;quot; );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your scripting language is object based (like JavaScript) or object-oriented, then you can invoke a script method on a script object. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code java&amp;gt;&lt;br /&gt;
import javax.script.*;&lt;br /&gt;
&lt;br /&gt;
public class InvokeScriptMethod {&lt;br /&gt;
    public static void main(String[] args) throws Exception {&lt;br /&gt;
        ScriptEngineManager manager = new ScriptEngineManager();&lt;br /&gt;
        ScriptEngine engine = manager.getEngineByName(&amp;quot;JavaScript&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        // JavaScript code in a String. This code defines a script object 'obj'&lt;br /&gt;
        // with one method called 'hello'.        &lt;br /&gt;
        String script = &amp;quot;var obj = new Object(); obj.hello = function(name) { print('Hello, ' + name); }&amp;quot;;&lt;br /&gt;
        // evaluate script&lt;br /&gt;
        engine.eval(script);&lt;br /&gt;
&lt;br /&gt;
        // javax.script.Invocable is an optional interface.&lt;br /&gt;
        // Check whether your script engine implements or not!&lt;br /&gt;
        // Note that the JavaScript engine implements Invocable interface.&lt;br /&gt;
        Invocable inv = (Invocable) engine;&lt;br /&gt;
&lt;br /&gt;
        // get script object on which we want to call the method&lt;br /&gt;
        Object obj = engine.get(&amp;quot;obj&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        // invoke the method named &amp;quot;hello&amp;quot; on the script object &amp;quot;obj&amp;quot;&lt;br /&gt;
        inv.invokeMethod(obj, &amp;quot;hello&amp;quot;, &amp;quot;Script Method !!&amp;quot; );&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementing Java Interfaces by Scripts===&lt;br /&gt;
Instead of calling specific script functions from Java, sometimes it is convenient to implement a Java interface by script functions or methods. Also, by using interfaces we can avoid having to use the javax.script API in many places. We can get an interface implementor object and pass it to various Java APIs. The following example demonstrates implementing the java.lang.Runnable interface with a script. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code java&amp;gt;&lt;br /&gt;
import javax.script.*;&lt;br /&gt;
&lt;br /&gt;
public class RunnableImpl {&lt;br /&gt;
    public static void main(String[] args) throws Exception {&lt;br /&gt;
        ScriptEngineManager manager = new ScriptEngineManager();&lt;br /&gt;
        ScriptEngine engine = manager.getEngineByName(&amp;quot;JavaScript&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        // JavaScript code in a String&lt;br /&gt;
        String script = &amp;quot;function run() { println('run called'); }&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
        // evaluate script&lt;br /&gt;
        engine.eval(script);&lt;br /&gt;
&lt;br /&gt;
        Invocable inv = (Invocable) engine;&lt;br /&gt;
&lt;br /&gt;
        // get Runnable interface object from engine. This interface methods&lt;br /&gt;
        // are implemented by script functions with the matching name.&lt;br /&gt;
        Runnable r = inv.getInterface(Runnable.class);&lt;br /&gt;
&lt;br /&gt;
        // start a new thread that runs the script implemented&lt;br /&gt;
        // runnable interface&lt;br /&gt;
        Thread th = new Thread(r);&lt;br /&gt;
        th.start();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
If your scripting language is object-based or object-oriented, it is possible to implement a Java interface by script methods on script objects. This avoids having to call script global functions for interface methods. The script object can store the &amp;quot;state&amp;quot; associated with the interface implementor. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code java&amp;gt;&lt;br /&gt;
import javax.script.*;&lt;br /&gt;
&lt;br /&gt;
public class RunnableImplObject {&lt;br /&gt;
    public static void main(String[] args) throws Exception {&lt;br /&gt;
        ScriptEngineManager manager = new ScriptEngineManager();&lt;br /&gt;
        ScriptEngine engine = manager.getEngineByName(&amp;quot;JavaScript&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        // JavaScript code in a String&lt;br /&gt;
        String script = &amp;quot;var obj = new Object(); obj.run = function() { println('run method called'); }&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
        // evaluate script&lt;br /&gt;
        engine.eval(script);&lt;br /&gt;
&lt;br /&gt;
        // get script object on which we want to implement the interface with&lt;br /&gt;
        Object obj = engine.get(&amp;quot;obj&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        Invocable inv = (Invocable) engine;&lt;br /&gt;
&lt;br /&gt;
        // get Runnable interface object from engine. This interface methods&lt;br /&gt;
        // are implemented by script methods of object 'obj'&lt;br /&gt;
        Runnable r = inv.getInterface(obj, Runnable.class);&lt;br /&gt;
&lt;br /&gt;
        // start a new thread that runs the script implemented&lt;br /&gt;
        // runnable interface&lt;br /&gt;
        Thread th = new Thread(r);&lt;br /&gt;
        th.start();&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Multiple Scopes for Scripts===&lt;br /&gt;
In the script variables example, we saw how to expose application objects as script global variables. It is possible to expose multiple global &amp;quot;scopes&amp;quot; for scripts. A single scope is an instance of javax.script.Bindings. This interface is derived from java.util.Map&amp;lt;String, Object&amp;gt;. A scope a set of name-value pairs where name is any non-empty, non-null String. Multiple scopes are supported by javax.script.ScriptContext interface. A script context supports one or more scopes with associated Bindings for each scope. By default, every script engine has a default script context. The default script context has atleast one scope called &amp;quot;ENGINE_SCOPE&amp;quot;. Various scopes supported by a script context are available through getScopes method. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code java&amp;gt;&lt;br /&gt;
import javax.script.*;&lt;br /&gt;
&lt;br /&gt;
public class MultiScopes {&lt;br /&gt;
    public static void main(String[] args) throws Exception {&lt;br /&gt;
        ScriptEngineManager manager = new ScriptEngineManager();&lt;br /&gt;
        ScriptEngine engine = manager.getEngineByName(&amp;quot;JavaScript&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        engine.put(&amp;quot;x&amp;quot;, &amp;quot;hello&amp;quot;);&lt;br /&gt;
        // print global variable &amp;quot;x&amp;quot;&lt;br /&gt;
        engine.eval(&amp;quot;println(x);&amp;quot;);&lt;br /&gt;
        // the above line prints &amp;quot;hello&amp;quot;&lt;br /&gt;
&lt;br /&gt;
        // Now, pass a different script context&lt;br /&gt;
        ScriptContext newContext = new SimpleScriptContext();&lt;br /&gt;
        Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE);&lt;br /&gt;
&lt;br /&gt;
        // add new variable &amp;quot;x&amp;quot; to the new engineScope        &lt;br /&gt;
        engineScope.put(&amp;quot;x&amp;quot;, &amp;quot;world&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
        // execute the same script - but this time pass a different script context&lt;br /&gt;
        engine.eval(&amp;quot;println(x);&amp;quot;, newContext);&lt;br /&gt;
        // the above line prints &amp;quot;world&amp;quot;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==JavaScript Script Engine==&lt;br /&gt;
Sun's implementation of JDK 6 is co-bundled with the Mozilla Rhino based JavaScript script engine. This is based on Mozilla Rhino version 1.6R2. Most of the Rhino implementation is included. A few components have been excluded due to footprint and security reasons: &lt;br /&gt;
&lt;br /&gt;
JavaScript-to-bytecode compilation (also called &amp;quot;optimizer&amp;quot;). This feature depends on a class generation library. The removal of this feature means that JavaScript will always be interpreted. The removal of this feature does not affect script execution because the optimizer is transparent. &lt;br /&gt;
Rhino's JavaAdapter has been removed. JavaAdapter is the feature by which a Java class can be extended by JavaScript and Java interfaces may be implemented by JavaScript. This feature also requires a class generation library. We have replaced Rhino's JavaAdapter with Sun's implementation of the JavaAdapter. In Sun implementation, only a single Java interface may be implemented by a JavaScript object. For example, the following works as expected. &lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
   var v = new java.lang.Runnable() {&lt;br /&gt;
      run: function() { print('hello'); &lt;br /&gt;
   }&lt;br /&gt;
   v.run();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In most cases, JavaAdapter is used to implement aa single interface with Java anonymizer class-like syntax. The uses of JavaAdapter to extend a Java class or to implement multiple interfaces are very rare. &lt;br /&gt;
E4X (ECMAScript for XML - ECMA Standard 357) has been excluded. Use of an XML literal in JavaScript code will result in a syntax error. Note that E4X support is optional in the ECMAScript standard - a implementation can omit E4X support and still be a compliant ECMAScript implementation. &lt;br /&gt;
The Rhino command line tools (Rhino shell, debugger etc.) are not included. But, you can use jrunscript instead. &lt;br /&gt;
&lt;br /&gt;
==JavaScript to Java Communication==&lt;br /&gt;
For the most part, accessing Java classes, objects and methods is straightforward. In particular field and method access from JavaScript is the same as it is from Java. We highlight important aspects of JavaScript Java access here. For more details, please refer to http://www.mozilla.org/rhino/scriptjava.html. The following examples are JavaScript snippets accessing Java. This section requires knowledge of JavaScript. This section can be skipped if you are planning to use some other JSR-223 scripting language rather than JavaScript. &lt;br /&gt;
&lt;br /&gt;
===Importing Java Packages, Classes===&lt;br /&gt;
The built-in functions importPackage and importClass can be used to import Java packages and classes. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// Import Java packages and classes &lt;br /&gt;
// like import package.*; in Java&lt;br /&gt;
importPackage(java.awt);&lt;br /&gt;
// like import java.awt.Frame in Java&lt;br /&gt;
importClass(java.awt.Frame);&lt;br /&gt;
// Create Java Objects by &amp;quot;new ClassName&amp;quot;&lt;br /&gt;
var frame = new java.awt.Frame(&amp;quot;hello&amp;quot;);&lt;br /&gt;
// Call Java public methods from script&lt;br /&gt;
frame.setVisible(true);&lt;br /&gt;
// Access &amp;quot;JavaBean&amp;quot; properties like &amp;quot;fields&amp;quot;&lt;br /&gt;
print(frame.title);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The Packages global variable can be used to access Java packages. Examples: Packages.java.util.Vector, Packages.javax.swing.JFrame. Please note that &amp;quot;java&amp;quot; is a shortcut for &amp;quot;Packages.java&amp;quot;. There are equivalent shortcuts for javax, org, edu, com, net prefixes, so pratically all JDK platform classes can be accessed without the &amp;quot;Packages&amp;quot; prefix. &lt;br /&gt;
&lt;br /&gt;
Note that java.lang is not imported by default (unlike Java) because that would result in conflicts with JavaScript's built-in Object, Boolean, Math and so on. &lt;br /&gt;
&lt;br /&gt;
importPackage and importClass functions &amp;quot;pollute&amp;quot; the global variable scope of JavaScript. To avoid that, you may use JavaImporter. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// create JavaImporter with specific packages and classes to import&lt;br /&gt;
&lt;br /&gt;
var SwingGui = new JavaImporter(javax.swing,&lt;br /&gt;
                            javax.swing.event,&lt;br /&gt;
                            javax.swing.border,&lt;br /&gt;
                            java.awt.event);&lt;br /&gt;
with (SwingGui) {&lt;br /&gt;
    // within this 'with' statement, we can access Swing and AWT&lt;br /&gt;
    // classes by unqualified (simple) names.&lt;br /&gt;
&lt;br /&gt;
    var mybutton = new JButton(&amp;quot;test&amp;quot;);&lt;br /&gt;
    var myframe = new JFrame(&amp;quot;test&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/code&lt;br /&gt;
&lt;br /&gt;
===Creating and Using Java Arrays===&lt;br /&gt;
While creating a Java object is the same as in Java, to create Java arrays in JavaScript we need to use Java reflection explicitly. But once created the element access or length access is the same as in Java. Also, a script array can be used when a Java method expects a Java array (auto conversion). So in most cases we don't have to create Java arrays explicitly. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// create Java String array of 5 elements&lt;br /&gt;
var a = java.lang.reflect.Array.newInstance(java.lang.String, 5);&lt;br /&gt;
&lt;br /&gt;
// Accessing elements and length access is by usual Java syntax&lt;br /&gt;
a[0] = &amp;quot;scripting is great!&amp;quot;;&lt;br /&gt;
print(a.length);&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Implementing Java Interfaces===&lt;br /&gt;
A Java interface can be implemented in JavaScript by using a Java anonymous class-like syntax: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
var r  = new java.lang.Runnable() {&lt;br /&gt;
    run: function() {&lt;br /&gt;
        print(&amp;quot;running...\n&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
};&lt;br /&gt;
// &amp;quot;r&amp;quot; can be passed to Java methods that expect java.lang.Runnable&lt;br /&gt;
var th = new java.lang.Thread(r);&lt;br /&gt;
th.start();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
When an interface with a single method is expected, you can pass a script function directly.(auto conversion) &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
function func() {&lt;br /&gt;
     print(&amp;quot;I am func!&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
// pass script function for java.lang.Runnable argument&lt;br /&gt;
var th = new java.lang.Thread(func);&lt;br /&gt;
th.start();&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Overload Resolution===&lt;br /&gt;
Java methods can be overloaded by argument types. In Java, overload resolution occurs at compile time (performed by javac). When calling Java methods from a script, the script interpreter/compiler needs to select the appropriate method. With the JavaScript engine, you do not need to do anything special - the correct Java method overload variant is selected based on the argument types. But, sometimes you may want (or have) to explicitly select a particular overload variant. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;code javascript&amp;gt;&lt;br /&gt;
var out = java.lang.System.out;&lt;br /&gt;
&lt;br /&gt;
// select a particular println function &lt;br /&gt;
out[&amp;quot;println(java.lang.Object)&amp;quot;](&amp;quot;hello&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/code&amp;gt;&lt;br /&gt;
More details on JavaScript's Java method overload resolution is at http://www.mozilla.org/js/liveconnect/lc3_method_overloading.html &lt;br /&gt;
&lt;br /&gt;
==Implementing Your Own Script Engine==&lt;br /&gt;
We will not cover implementation of JSR-223 compliant script engines in detail. Minimally, you need to implement the javax.script.ScriptEngine and javax.script.ScriptEngineFactory interfaces. The abstract class javax.script.AbstractScriptEngine provides useful defaults for a few methods of the ScriptEngine interface. &lt;br /&gt;
&lt;br /&gt;
Before starting to implement a JSR-223 engine, you may want to check http://scripting.dev.java.net project. This project maintains JSR-223 implementations for many popular open source scripting languages. &lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
* [http://jcp.org/en/jsr/detail?id=223 JSR-223 Scripting for the Java Platform]&lt;br /&gt;
* [http://java.sun.com/javascript JavaScript Developer Connection]&lt;br /&gt;
* [http://www.mozilla.org/js/liveconnect/lc3_method_overloading.html Java Method Overloading and LiveConnect 3]&lt;br /&gt;
* [http://www.mozilla.org/rhino/ Rhino:JavaScript for Java]&lt;br /&gt;
* [http://www.mozilla.org/rhino/scriptjava.html Scripting Java (from JavaScript)]&lt;br /&gt;
* [https://scripting.dev.java.net/ scripting.dev.java.net project]&lt;/div&gt;</summary>
		<author><name>Rimondini</name></author>
		
	</entry>
</feed>