How to put custom ClassLoader to use?

Not sure I understand the question, you have a 3rd party lib and you want it to use your classloader to load classes. If you’re lucky the third party lib uses the threads context classloader which you can set using Thread.currentThread().setContextClassLoader(myClassLoader), in the same thread you can access this classloader with Thread.currentThread().getContextClassLoader()… Another point, but … Read more

sysLoader.getResource() problem in java

This is known by Sun/Oracle, their advice is to use URI objects which will remove the %20 characters: Instead of doing this: FileInputStream fis = new FileInputStream(url.getFile()); you can force any %-escaped characters to be decoded by first converting the URL to a URI, and then use the path component of the URI as the … Read more

What does the sun.reflect.CallerSensitive annotation mean?

According to the JEP I linked to in the comments (also here), A caller-sensitive method varies its behavior according to the class of its immediate caller. It discovers its caller’s class by invoking the sun.reflect.Reflection.getCallerClass method. If you look at the implementation of Class#forName(String) @CallerSensitive public static Class<?> forName(String className) throws ClassNotFoundException { return forName0(className, … Read more

How to diagnose a Java 8 metaspace leak?

The main cause for the java.lang.OutOfMemoryError: Metaspace is: either too many classes or too big classes being loaded to the Metaspace. If you want to recreate the problem use this code snippet: public class Metaspace { static javassist.ClassPool cp = javassist.ClassPool.getDefault(); public static void main(String[] args) throws Exception { for (int i = 0; ; … Read more

Java : in what order are static final fields initialized?

Yes, they are initialized in the order in which they appear in the source. You can read all of the gory details in The Java Language Specification, §12.4.2. See step 9, which reads: … execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual … Read more

Strange behavior of Class.getResource() and ClassLoader.getResource() in executable jar

I thought this question was already asked and answered! What is the difference between Class.getResource() and ClassLoader.getResource()? getClass().getResource() searches relative to the .class file while getClass().getClassLoader().getResource() searches relative to the classpath root. If there’s an SSCCE here, I don’t understand why it doesn’t 1) Show the directory organization in the .jar, and… 2) Take package … Read more

How do I create a parent-last / child-first ClassLoader in Java, or How to override an old Xerces version that was already loaded in the parent CL?

Today is your lucky day, as I had to solve this exact problem. I warn you though, the innards of class loading are a scary place. Doing this makes me think that the designers of Java never imagined that you might want to have a parent-last classloader. To use just supply a list of URLs … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)