How Do You Invoke the main Section of a Groovy Program?

Problem scenario
Your Groovy code has a section like this:

class Example {
static void main(String[] args) {

But this section above is not executing when you run your Groovy program. There are no errors however. How do you get it to execute?

Possible Solution #1
Remove lines of code (or statements) that are outside the “class Example {…”

Possible Solution #2
See this posting: How Do You Invoke a Class in a Groovy Program?

 » Read more..

How Do You Troubleshoot an Error like This in Groovy “groovy.lang.MissingMethodException”?

Problem scenario
You run a Groovy program like this: groovy foobar.groovy

You see this message:

Caught: groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (foobar$_run_closure5$_closure6) values: [foobar$_run_closure5$_closure6@6150c3ec]
Possible solutions: wait(), any(), wait(long), and(java.lang.Boolean), each(groovy.lang.Closure), any(groovy.lang.Closure)
groovy.lang.MissingMethodException: No signature of method: java.lang.Boolean.call() is applicable for argument types: (foobar$_run_closure5$_closure6) values: [foobar$_run_closure5$_closure6@6150c3ec]
Possible solutions: wait(), any(), wait(long), and(java.lang.Boolean), each(groovy.lang.Closure), any(groovy.lang.Closure)
at foobar$_run_closure5.doCall(foobar.groovy:56)

What should you do?

 » Read more..

How Write a Custom Method inside of a Class in Groovy?

Problem scenario
You are familiar with Groovy scripts. Now you want to use a class and a method in the class. How do you do this?

Solution
Prerequisites
Install Groovy. See these directions for Ubuntu/Debian if you need assistance; see these directions for CentOS.

Procedures

  1. Create a file called test.groovy with the content below:

class Example {
def static Display(nifty) {
nifty.call(“Red”);  » Read more..

How Do You Invoke a Function in Groovy?

Problem scenario
You want to call a function and have it return a value. How do you do this?

Solution
Overview
(If you need assistance installing Groovy, see this posting for CentOS. or see this posting for Debian/Ubuntu Linux.)

Groovy has something called “Closures.” These act like functions in other programming languages.

 » Read more..

How Do You Troubleshoot the Groovy Error “JAVA_HOME is not defined correctly”?

Problem scenario
You try to run a Groovy command (e.g., groovy foobar.groovy), but you get this error:
“groovy: JAVA_HOME is not defined correctly, can not execute: /usr/lib/jvm/java-9-openjdk/bin/java”

What should you do?

Solution

  1. Set the JAVA_HOME variable. Run this command: which java
  2. Mentally eliminate the trailing “/bin/java” from the above command. If Java was not found, follow these directions to install Java.

 » Read more..

What is a Multi-branch Jenkins Job?

Question
With a newer version of Jenkins, you want to know what a multi-branch job is. What is a multi-branch Jenkins job?

Answer
With a given repository of Git code, you may have different branches. These branches may be for new features of a compiled program, slight changes in code (e.g., to fix a bug or make a procedure run more quickly), or to completely refactor an existing program.

 » Read more..

How Do You Write a Tic-Tac-Toe Program in Groovy?

Problem scenario
You want to code a Groovy 2.x program.  You want a program that reads user input, prints out processed output, uses closures (similar to functions in other languages) with and without return statements, handles some exceptions (non-standard input or input that is no longer valid based on what was previously entered), and uses classes.  How do you write a Tic-Tac-Toe (aka naughts and crosses) program in Groovy that has the above things?

 » Read more..

How Do You Invoke a class in a Groovy Program?

Problem scenario
You have a Groovy with these lines of code:

class Contint {
   static void main(String[] args) {
      def apple = {println “This is a basic test.”};
      apple.call();
   }
}
println “This is the second print statement in the code.”

The “Contint” class is never invoked.  What should you do?

Solution
Overview

You need to use the “new” reserved word. 

 » Read more..

How Do You Troubleshoot “Caught: java.lang.ArrayStoreException: org.codehaus.groovy.runtime.GStringImpl” when running a Groovy Program?

Problem scenario
You are running a Groovy program. 

Here is your code:

def input = System.console().readLine ‘Please provide some input: ‘
def x = input
def boardArray = new String[2]
boardArray[0] = “”
boardArray[1] = “”
boardArray[1] = “$x”
println boardArray[1]

When you run it (i.e., with groovy foobar.groovy), you get this error:

“Caught: java.lang.ArrayStoreException: org.codehaus.groovy.runtime.GStringImpl
java.lang.ArrayStoreException: org.codehaus.groovy.runtime.GStringImpl”

What should you do?

 » Read more..

When Using Jenkins, What Are the Differences between a Declarative Pipeline and a Scripted Pipeline?

Question
In the context of Jenkinsfiles, there are two types of pipelines: declarative and scripted.  What are the differences between these two types?

Answer
1.  The syntax of the two is one difference.  While both are based on Groovy, Declarative Pipeline syntax is more simple (according to this posting).  Declarative Pipeline syntax follows more of an declarative paradigm whereas Scripted Pipeline syntax follows more of a imperative paradigm (according to this posting).

 » Read more..