Skip to content
Skip to breadcrumbs
Skip to header menu
Skip to action menu
Skip to quick search
Quick Search
Browse
Pages
Blog
Labels
Attachments
Mail
Advanced
What’s New
Space Directory
Feed Builder
Keyboard Shortcuts
Confluence Gadgets
Log In
Sign Up
Dashboard
Groovy
Copy Page
You are not logged in. Any changes you make will be marked as
anonymous
. You may want to
Log In
if you already have an account. You can also
Sign Up
for a new account.
This page is being edited by
.
Paragraph
Paragraph
Heading 1
Heading 2
Heading 3
Heading 4
Heading 5
Heading 6
Preformatted
Quote
Bold
Italic
Underline
More colours
Strikethrough
Subscript
Superscript
Monospace
Clear Formatting
Bullet list
Numbered list
Outdent
Indent
Align left
Align center
Align right
Link
Table
Insert
Insert Content
Image
Link
Attachment
Symbol
Emoticon
Wiki Markup
Horizontal rule
tinymce.confluence.insert_menu.macro_desc
Info
JIRA Issue
Status
Gallery
Tasklist
Table of Contents
Other Macros
Page Layout
No Layout
Two column (simple)
Two column (simple, left sidebar)
Two column (simple, right sidebar)
Three column (simple)
Two column
Two column (left sidebar)
Two column (right sidebar)
Three column
Three column (left and right sidebars)
Undo
Redo
Find/Replace
Keyboard Shortcuts Help
<p>Java 6 includes built-in support for <a href="http://www.jcp.org/en/jsr/detail?id=223">JSR 223: Scripting for the Java Platform</a> API classes. This framework can be used to host Script Engines in Java Applications. Numerous Scripting engines are <a href="https://scripting.dev.java.net/">available</a>.</p><p>Here is how you can use JSR 223 to talk to Groovy from Java:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>// require(url:'https://scripting.dev.java.net', jar:'groovy-engine.jar') import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; public class CalcMain { public static void main(String[] args) throws Exception { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("groovy"); // basic example System.out.println(engine.eval("(1..10).sum()")); // example showing scripting variables engine.put("first", "HELLO"); engine.put("second", "world"); System.out.println(engine.eval("first.toLowerCase() + second.toUpperCase()")); } } </pre></td></tr></table><p>The output is:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>55 helloWORLD </pre></td></tr></table><p>To make this example work, if you're running Groovy 1.5.x, add <code>groovy-engine.jar</code> from <a href="https://scripting.dev.java.net/">https://scripting.dev.java.net/</a> to your CLASSPATH and run this program using Java 6. But since Groovy 1.6, Groovy already contains the groovy scripting engine compliant with JSR-223.</p><p>This next example illustrates calling an invokable function:</p><table class="wysiwyg-macro" data-macro-name="code" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGV9&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>// require(url:'https://scripting.dev.java.net', jar:'groovy-engine.jar') import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.Invocable; public class FactMain { public static void main(String[] args) throws Exception { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("groovy"); String fact = "def factorial(n) { n == 1 ? 1 : n * factorial(n - 1) }"; engine.eval(fact); Invocable inv = (Invocable) engine; Object[] params = { new Integer(5) }; Object result = inv.invokeFunction("factorial", params); System.out.println(result); } } </pre></td></tr></table><p>And when run, results in:</p><table class="wysiwyg-macro" data-macro-name="code" data-macro-default-parameter="none" style="background-image: url(/plugins/servlet/confluence/placeholder/macro-heading?definition=e2NvZGU6bm9uZX0&locale=en_GB&version=2); background-repeat: no-repeat;" data-macro-body-type="PLAIN_TEXT"><tr><td class="wysiwyg-macro-body"><pre>120 </pre></td></tr></table><p>Groovy has many mechanisms for integration with Java, some of which provider richer options than available with JSR 223 (e.g. greater configurability and more security control). JSR 223 is recommended when you need to keep the choice of language used flexible and you don't require integration mechanisms not supported by JSR-223.</p><p>The engine keeps per default hard references to the script functions. To change this you should set a engine level scoped attribute to the script context of the name <em>"#jsr223.groovy.engine.keep.globals"</em> with a String being "phantom" to use phantom references, "weak" to use weak references or "soft" to use soft references - casing is ignored. Any other string will cause the use of hard references.</p><p>See also: <a class="confluence-link" href="/display/GROOVY/JSR-223+access+to+other+JVM+languages" data-linked-resource-id="79528444" data-linked-resource-type="page" data-linked-resource-default-alias="JSR-223 access to other JVM languages" data-base-url="http://docs.codehaus.org">JSR-223 access to other JVM languages</a></p>
Please type the word appearing in the picture.
Attachments
Labels
Location
Watch this page
< Edit
Preview >
Loading…
Save
Cancel
Next hint
search
attachments
weblink
advanced