How to get a List of Resources From a Directory in Java Classpath

You can use Reflections library for doing this. Reflections is a open source Java library. It scans Java classpath and indexes it with metadata. This library allows you to query the classpath at runtime and can be very handy for many run-time reflection code needs. In Java, if you want to get a list of resources from … Read more

Why Calling System.setProperty() does not Affect the Classpath at Run-Time

You can easily set any system properties in java using System.setPropoerty method, However it may not have any effect in case of CLASSPATH property. This is mainly because the Java system class loader is initialized very early in the JVM startup sequence. The class loader copies the classpath into its own data structures, and the … Read more

How to Find the Load Location of a Java Class File at Run-Time

There are two ways to find it: Using Classloader Below code snippet can be used to find the location of java class com.fromdev.MyClass this.getClass().getClassLoader().getResource(“com/fromdev/MyClass.class”)   Using Protection Domain We can use this to find the exact location a jar file containing the class JVM is using clazz.getProtectionDomain().getCodeSource().getLocation() How Java handles Two classes with same name … Read more

How to find which Jar File is being used by Java Run-Time

On Windows You need use below windows program Process Explorer that lets you see which files are open for a particular process or program   On Unix, Linux or Mac It can be done using lsof command. lsof is one of my favorite and useful java debugging commands on Unix. Below is the syntax for using this command: … Read more

How to Read a File from CLASSPATH in Java

This can be done in two simple ways   Using ClassLoader.getResourceAsStream This method can be used to load any file from CLASSPATH InputStream in =this.getClass().getClassLoader().getResourceAsStream(“MyFile.txt”);   Using Class.getResourceAsStream This method can be used to load files using relative path to the package of the class InputStream in =this.getClass().getResourceAsStream(“SomeTextFile.txt”);   This method can also be used to load any files … Read more