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>When writing a script, it may be unwieldy to call the script defining the whole classpath at the command line, e.g. </p> <p> groovy -cp %JAXB_HOME%\bin\activation.jar;%JAXB_HOME%\bin\... myscript.groovy</p> <p>You can go the other way - let the script itself find the jars it needs and add them to the classpath before using them. To do this, you need to </p> <p>1. get the groovy rootloader</p> <p>def loader = this.class.classLoader.rootLoader</p> <p>2. introduce the necessary uls to groovy rootloader. Use whatever logic suits your situations to find the jars / class directories</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> def jardir = new File( System.getenv( 'JAXB_HOME' ), 'lib' ) def jars = jardir.listFiles().findAll { it.name.endsWith('.jar') } jars.each { loader.addURL(it.toURI().toURL()) } </pre></td></tr></table> <p>3. Load the classes you need:</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> // in a script run from command line this is ok: JAXBContext = Class.forName( 'javax.xml.bind.JAXBContext' ) Marshaller = Class.forName( 'javax.xml.bind.Marshaller' ) // if the groovy script / class is loaded from a java app, then the above may fail as it uses the same classloader to load the class as the containing script / class was loaded by. In that case, this should work: JAXBContext = Class.forName( 'javax.xml.bind.JAXBContext', true, loader ) Marshaller = Class.forName( 'javax.xml.bind.Marshaller', true, loader ) </pre></td></tr></table> <p>4. To instantiate the classes, use the newInstance method:</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> def jaxbContext = JAXBContext.newInstance( MyDataClass ) </pre></td></tr></table> <p>Note that newInstance is on steroids when called from groovy. In addition to being able to call the parameterless constructor (as w/ Java's Class.newInstance()), you can give any parameters to invoke any constructor, e.g.</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> def i = MyClass.newInstance( "Foo", 12 ) // invokes the constructor w/ String and int as params </pre></td></tr></table> <p>You can also pass a map to initialize properties, e.g.</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> def i2 = MyClass.newInstance(foo:'bar', boo:12) // creates a new instance using the parameterless constructor and then sets property foo to 'bar' and property boo to 12 </pre></td></tr></table> <p>The downside of using this approach is that you can't inherit from the classes you load this way - classes inherited from need to be known before the script starts to run.</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