Meaningless Drivel is fun!*
The moose likes Groovy and the fly likes Groovy MultiLine strings and Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Groovy MultiLine strings and Java" Watch "Groovy MultiLine strings and Java" New topic

Groovy MultiLine strings and Java

Paul Ristevian

Joined: Jun 24, 2009
Posts: 12
Hello there,
I wish to write Java code and Groovy together interacting.
So this is the situation I wish to create.

1. Have a java code to connect (via JDBC) to a database and retrieve data into a java recordset/ Array.

2. Second I wish to pass some elements of the Java Array to some Groovy variables
For example:

GroovyVar Name = Array[0] (java variable this side)
GroovyVar Telephone = Array[2], etc.

3. Next these Groovy variable will be used with GStrings in a MultiLine String.


GroovyVar StringMulti = ''' Hello there ${Name},
is your Telephone Number : ${Telephone} and is
the area code a European code?"""

4. Next I wish to pass this Groovy MultiString to a Java String variable.

JavaString StringJav = StringMulti;

So you see my main aim is to have/use MultiString in Java.

Can someone tell me how to accomplish this above task.

So first I pass Java-->Groovy, then from Groovy-->Java.

Greatly appreciate it if someone could help me on passing java and groovy variables back and forth
in a Java file.

I am a newbie to Groovy.(hey that rhymes)


Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299

So the first step is understanding how Groovy code and Java code can be used together. Groovy code can be called from Java code a couple of different ways...

1. Groovy classes are compiled to byte code. You can then include these classes in your classpath and treat them as first class Java citizens. So if you had a Groovy class called User you could simply say User user = new User() in Java.

2. Groovy scripts can be executed from Java code. It is possible to retrieve data from the script through the engine that you execute in Java.

There is really only 1 way to use Java code from groovy and that is to have the classes in your classpath when executing Groovy code.

So my question is, which path(s) do you prefer?

GenRocket - Experts at Building Test Data
Paul Ristevian

Joined: Jun 24, 2009
Posts: 12
Wow thanks for the quick reply.

I think the first method is to my liking that you mention,
but then can I pass the Groovy variable(the multiline string) back to a Java String,
that is my question.
Is it easy?

I personally think that if someone was to show, give a straight forward example in both techniques
that you mention, would be very good.

Hope to hear from you again.


Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299

So if I understand what you are wanting, let me show this..FYI, totally untested..

Paul Ristevian

Joined: Jun 24, 2009
Posts: 12
Wow, that looks like good stuff.

Could one though, do something like this :
You have a hardcoded string here:


What I would like to see is the data in a variable, say :
String Arr[0] = "Gregg",



If I can pass variable this way, then you have very quickly given me
the "Holy Grail" of Groovy-Java I have been looking for, for over a month!!!

Really appreciate all your help.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Groovy MultiLine strings and Java
Similar Threads
Java-Groovy : passing arrays
Sun Cirtification
Counting words in a String
Groovy : Process CSV to SQL