How to Add A Jar File To Java System Classpath At Run-Time

This can be done by using a simple reflection API hack as demonstrated in below sample code. This example assumes you have a file “c:/Sample.txt” that is not already in class path and at run-time c:/ is added the System classpath and then Sample.txt is made available. import java.io.File; import java.io.InputStream; import java.lang.reflect.Method; import java.net.URL; import java.net.URLClassLoader;   public class HackJavaClasspath {   … Read more

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