Running Java Class from Terminal
How to run Java class in Terminal
This is to create and run one or more java files on terminal that is depends on third-party jars.
This helps to quickly test some features on real environment without running E2E flow of your application.
-
Write java file that you want to run
FileCleanupUtil.java
package codergists;
import org.apache.commons.io.filefilter.AgeFileFilter;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.stream.Stream;
import static java.time.LocalDate.now;
import static java.time.ZoneId.systemDefault;
import static java.util.Date.from;
import static org.apache.commons.io.FileUtils.deleteDirectory;
import static org.apache.commons.io.FileUtils.listFilesAndDirs;
/**
* Utility class cleanup archived session files which are older than 2 weeks
*
* @author tvajjala
*/
public class FileCleanupUtil {
/**
* Threshold time
*/
private static final int THRESHOLD = 2;//weeks
/**
* cutOff date
*/
private static final Date cutoffDate = from(now().minusWeeks(THRESHOLD).atStartOfDay(systemDefault()).toInstant());
/**
* ThreadShould date filter
*/
private static AgeFileFilter ageFileFilter = new AgeFileFilter(cutoffDate);
/**
* Directories to search for older files
*/
private static String[] DEFAULT_DIRS = new String[]{"/dir1/", "/dir2/dir3/"};
/**
* Scan Directory
*
* @param parentDir
*/
static void scanDirectory(File parentDir) {
System.out.println("Searching old files under" + parentDir + "with cutoff date" + cutoffDate);
Collection<File> list = listFilesAndDirs(parentDir, ageFileFilter, ageFileFilter);
list.stream().filter(f -> !f.isDirectory())
.forEach(f -> System.out.println(f + " deleted ?" + f.delete()));
list.stream().filter(dir -> dir.isDirectory() && !dir.equals(parentDir))
.forEach(dir -> {
System.out.println("Deleting dir " + dir);
scanDirectory(dir);
try {
if (dir.list().length != 0) {
System.out.println(dir + " directory not empty");
return;
}
deleteDirectory(dir);
} catch (IOException ioException) {
System.out.println("Failed to delete directory" + dir);
}
});
}
static void cleanupOldFiles(String... parentDirs) {
Stream.of(parentDirs)
.map(dir -> new File(dir))
.forEach(parentDir -> scanDirectory(parentDir));
}
/**
* Public API Specify default parent directories
*/
public static void deleteOldFiles() {
cleanupOldFiles(DEFAULT_DIRS);
}
/**
* Specify command line arguments (standalone mode)
*
* @param args list of directories
*/
public static void main(String[] args) {
cleanupOldFiles(args);
}
}
-
Compile java file
Compile java file and place it under right directory
NOTE: -d .
option directory we specified using package
compile.sh
javac -cp lib/a.jar:lib/b.jar:lib/c.jar -d . FileCleanupUtil.java
javac -cp lib/commons-io-2.8.0.jar:lib/slf4j-api-1.7.25.jar -d . FileCleanupUtil.java
-
Create temporary jar with your classes
jar.sh
jar -cvf file-filter.jar codergists/*
# To verify content inside jar
jar -tf file-filter.jar
-
Run program
run.sh
java -cp file-filter.jar:lib/* codergists.FileCleanupUtil /path/to/parent/dir /path/2nd/dir
-
To supply log configuration
logConfig.sh
java -Dlog4j.configurationFile=log4j2.xml -cp file-filter.jar:lib/* codergists.FileCleanupUtil /path/to/parent/dir /path/2nd/dir
Comments
Post a Comment