Showing posts with label Groovy. Show all posts
Showing posts with label Groovy. Show all posts

Friday, April 5, 2019

Java 11 and Groovy Compatibility and JAXBContext !

If you see the following error during compile : 

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':compileTestGroovy'.
> org/codehaus/groovy/ast/MethodCallTransformation

Add the folling line to your gradle file :

ext['groovy.version'] = '2.5.6'  //i.e your grovy version.

Another issues with java 11

 Unable to load class groovy.xml.jaxb.Jaxb GroovyMethods due to missing dependency javax/xml/bind/JAXBContext 

You can fix this by adding the following dependency on the latest groovy version


Tuesday, November 3, 2009

Converting Groovy Array type to Java Array Type

Following is the code snipped in groovy that demonstrate the conversion Groovy Array type to Java Array Type.
Code for Groovy

def param=[]
param+="paramater 1"
param+="paramater 2"
param+="paramater 3"
param+="paramater 4"
def stringArgForJava = (String[])params

Test.callJavaClass(stringArgForJava) //static method

The sample java class is as follow;

class Test
public static void callJavaClass(String[] arg)
for(int i=0;i {
System.out.println("params "+i ":" +params[i]);



Tuesday, October 13, 2009

Read a variable defined on the config file from a controller in a grail application

Let's say, you have the config.groovy file as follow.


environments {
production {
myVariable1="I am variable1 on RPOD"
myVariable1="I am variable1 on RPOD"

development {
myVariable1="I am variable1 on DEV"
myVariable1="I am variable1 on RPOD"

log4j {
appender.stdout = "org.apache.log4j.ConsoleAppender"

Now the above varaible can be easily access through any of the controller as follow.

def config = ConfigurationHolder.config
def myVaraible1 = config.myVaraible1
def myVaraible2 = config.myVaraible2


Wednesday, July 8, 2009

Reading the status code of HTTP Request !

When you make an HTTP request to any server url throuh your client software, lets say IE, HTTP Status codes are returned by the server to the client software to determine the outcome of your request. There are five different classes of status code range. They are
Status codes 100-101
Status codes 200-206
Status codes 300-307
Status codes 400-417
Status codes 500-505

Following is a simple program written in groovy that checks and returns the status code of any url.

def url
def message =""
boolean fail=true
def urlString=""
println checkURL(urlString)

def checkURL(urlString){
int status_code
def tempmessage=""
URL URLserver = new URL("urlString");
URLConnection connection = (HttpURLConnection)URLserver.openConnection();

status_code = connection.getResponseCode();

switch (status_code)
case 200:
tempmessage=" Connection Successful"
case 401:
case 405 :
tempmessage= " Resource Not Found"
case 408:
tempmessage="Request Time-out"
case 500:
tempmessage="Internal Server Error"
case 502:
tempmessage="Bad Gateway"
case 503:
tempmessage="Service Temporarily Unavailable"
default :
tempmessage= connection.getResponseMessage()

catch (Exception exc){


return "\n "+urlString+" :"+status_code+":"+tempmessage;


Visit for detail about the status codes.


Friday, January 30, 2009

Read Environment Variables with Groovy

Environment variables can be easily accessed with Groovy with getenv() method as shown below.

def env = System.getenv()
//Print all the environment variables.

println it
// You can also access the specific variable, say 'username', as show below
String user= env['USERNAME']


Wednesday, July 16, 2008

Generate Random Alphanumeric Random String of N Characters with Groovy or Java

I was googling for a Alpha-numeric Random String generation. I come up with two solution. One option is to generate Random Alphanumeric Strings with RandomStringUtils available on Apache Common . The following section of code is borrowed from

import org.apache.commons.lang.RandomStringUtils;
public class RandomStringUtilsTrial {
public static void main(String[] args) {

//Random 8 chars string where letters are enabled while numbers are not.
System.out.print("8 char string using letters but no numbers >>>");
System.out.println(RandomStringUtils.random(8, true, false));


Other Option can be using the Java Uitil class Random as shown below. You don’t have to worry about Apache Common API. The following code section (written for Groovy) generates 100 '32 characters' long Alphanumeric Random string.

private static String validChars ="ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"
private int IDlength=32
int maxIndex = validChars.length()
for(i in 0..99)
{ String resultID = ""
java.util.Random rnd = new java.util.Random(System.currentTimeMillis()*(new java.util.Random().nextInt()))
for ( i in 0..IDlength ) {
int rndPos = Math.abs(rnd.nextInt() % maxIndex);
resultID += validChars.charAt(rndPos)

println resultID

Good luck !

Wednesday, May 7, 2008

Read custom created XML file from a Groovy/Java class in a J2EE Web Application

I have a following xml file that I created to store the application name and Application Owner email address

//File name: EmailInfo.xml

< emailInfo>
< application> < name> Saleways < /name> < email>< /email> < /application>
< application> < name> Mercantile House< /name> < email>< /email> < /application>
< application> < name> BishowShop< /name> < email>< /email> < /application>
< /emailInfo>

Now I have a J2EE application that contains a Groovy or a Java class that access the above XML file. The method to access above html file using Groovy is presented below. Note that the xml file resides on the same location where the class exists.

def appName=[]
def appEmail=[] url= this.getClass().getClassLoader().getResource("EmailInfo.xml"); uri = new URI(url.toString())
File f=new File(uri)
def emailInfo = new XmlSlurper().parse(f) //Specific to groovy {appName += it} //specific to groovy {appEmail += it} // specific to groovy

You simply can modify the above code for java classes. The final result for the above code is that appName contains the list of the Name and appEmail contains the list of the Email address. You can put it into hash map as below.

def emailMap=[:] //define an empty map
for(i in 0 .. appName.size()-1)

Now you simply can access the email address of a person as shown below

println emailMap[“Mercantile House”] //prints



 © 2006-2010