Using Java Classes from Groovy

Using Java classes from Groovy is simple and direct. If the Java classes we want to use are part of the JDK, we import the classes or their packages in Groovy just like in Java. By default Groovy imports a number of packages and classes (see From Java to Groovy), so if the class we want to use is imported already (such as java.util.Date), then we just use it—no import is needed.

If we want to use one of our own Java classes, or classes that are not part of the standard JDK, we can import them in Groovy just like we would in Java. Make sure to import the necessary packages or classes, or refer to the classes by their fully qualified names. When running groovy, specify the path to the .class files or JARs using ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.