Over a million developers have joined DZone.
Silver Partner

Deleting All .class Files with Groovy

· Java Zone

The Java Zone is brought to you by Stormpath—offering a pre-built identity API for developers. Easily build powerful user management, authentication, and authorization into your web and mobile applications. Check out this tutorial to build a simple web app with Spring Boot and Spring Security in 15 minutes.

When running simple Java tests either to learn how something works in Java or to build examples for my blog posts, I often store the examples in the same directory on my machine. I decided that I wanted to remove all the generated .class files from this directory. There are several approaches to doing this on a Linux machine, but I wanted to remove these files on a Windows machine. I know there are tools and ways to do this on Windows, but I knew it would take me less time to write a simple Groovy script to do it than to search online for such a tool. This post provides and briefly describes the simple Groovy script for removing .class files.


#!/usr/bin/env groovy
if (args.length < 1)
   println "Please specify directory under which .class files should be removed"
   println "  (including .class files in sub-directories)."

def directoryName = args[0]
println "Remove .class files in ${directoryName} and its subdirectories..."
def directory = new File(directoryName)
def classPattern = ~/.*\.class/
{ file ->
   if (file ==~ classPattern)
      println "Deleting ${file}..."

The above script will remove all files that have names ending with a .class suffix in the provided directory or any of its sub-directories. The directory is specified via a single command-line argument to the script, so I did not use Groovy's built-in CLI support in this case and instead relied on Groovy's implicit availability of the args parameter representing the command line arguments.

The Groovy script featured in this post takes advantage of the Groovy GDK File extension's eachFileRecurse method to recursively iterate through the names of the files in the provided directory and its subdirectories. Another convenient Groovy feature employed in this script is application of Groovy regular expression support to specify the pattern (file names ending with ".class" extension) and then to use the pattern matcher in the conditional that decides which files are to be deleted.

The above script prints out the files to be deleted as it deletes them. One could easily enhance the script to accept a parameter that would display files that would be deleted without actually deleting them. For me, this script was more proof that Groovy is so easy to use that sometimes it's easier to write a new script in Groovy to accomplish daily tasks than it is to find existing tools and scripts online to download.


The Java Zone is brought to you by Stormpath—offering a pre-built, streamlined user management API for building web and mobile applications. Learn how token authentication with JWTs (JSON Web Tokens) helps secure websites and avoids the pitfalls of using cookies and sessions.


Published at DZone with permission of Dustin Marx , DZone MVB .

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}