Using Java classes from Groovy
The Groovy language is designed in a way that it fully supports the Java syntax (there are only few minor exceptions; for example, do..while
is not supported in Groovy). Most of the code that you can write in Java can automatically be considered Groovy code as well.
In this recipe, we will learn how simple it is to use existing Java classes and libraries from your Groovy code and we will explore some basic language features that makes Groovy—groovy!
How to do it...
This recipe has a rather simple setup. You can either start up the groovyConsole
, as described in the Starting groovyConsole to execute Groovy snippets recipe in Chapter 1, Getting Started with Groovy, or you can create a new file with the *.groovy
extension.
In the
groovyConsole
type the following code:import java.io.File; File currentDir = new File("."); System.out.println("Current directory: " + currentDir.getAbsolutePath()); File[] files = currentDir.listFiles(); for (File file: files) { System...