Unloading classes in java?

The only way that a Class can be unloaded is if the Classloader used is garbage collected. This means, references to every single class and to the classloader itself need to go the way of the dodo. One possible solution to your problem is to have a Classloader for every jar file, and a Classloader … Read more

What does JVM flag CMSClassUnloadingEnabled actually do?

Update This answer is relevant for Java 5-7, Java 8 has this fixed: https://blogs.oracle.com/poonam/about-g1-garbage-collector,-permanent-generation-and-metaspace Kudos go to mt.uulu For Java 5-7: The standard Oracle/Sun VM look on the world is: Classes are forever. So once loaded, they stay in memory even if no one cares anymore. This usually is no problem since you don’t have … Read more

URL to load resources from the classpath in Java

Intro and basic Implementation First up, you’re going to need at least a URLStreamHandler. This will actually open the connection to a given URL. Notice that this is simply called Handler; this allows you to specify java -Djava.protocol.handler.pkgs=org.my.protocols and it will automatically be picked up, using the “simple” package name as the supported protocol (in … Read more

What is the difference between Class.getResource() and ClassLoader.getResource()?

Class.getResource can take a “relative” resource name, which is treated relative to the class’s package. Alternatively you can specify an “absolute” resource name by using a leading slash. Classloader resource paths are always deemed to be absolute. So the following are basically equivalent: foo.bar.Baz.class.getResource(“xyz.txt”); foo.bar.Baz.class.getClassLoader().getResource(“foo/bar/xyz.txt”); And so are these (but they’re different from the above): … Read more

Scanning Java annotations at runtime

Use org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider API A component provider that scans the classpath from a base package. It then applies exclude and include filters to the resulting classes to find candidates. ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(<DO_YOU_WANT_TO_USE_DEFALT_FILTER>); scanner.addIncludeFilter(new AnnotationTypeFilter(<TYPE_YOUR_ANNOTATION_HERE>.class)); for (BeanDefinition bd : scanner.findCandidateComponents(<TYPE_YOUR_BASE_PACKAGE_HERE>)) System.out.println(bd.getBeanClassName());

Scanning Java annotations at runtime

Use org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider API A component provider that scans the classpath from a base package. It then applies exclude and include filters to the resulting classes to find candidates. ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(<DO_YOU_WANT_TO_USE_DEFALT_FILTER>); scanner.addIncludeFilter(new AnnotationTypeFilter(<TYPE_YOUR_ANNOTATION_HERE>.class)); for (BeanDefinition bd : scanner.findCandidateComponents(<TYPE_YOUR_BASE_PACKAGE_HERE>)) System.out.println(bd.getBeanClassName());

Difference between thread’s context class loader and normal classloader

This does not answer the original question, but as the question is highly ranked and linked for any ContextClassLoader query, I think it is important to answer the related question of when the context class loader should be used. Short answer: never use the context class loader! But set it to getClass().getClassLoader() when you have … Read more

How to load JAR files dynamically at Runtime?

The reason it’s hard is security. Classloaders are meant to be immutable; you shouldn’t be able to willy-nilly add classes to it at runtime. I’m actually very surprised that works with the system classloader. Here’s how you do it making your own child classloader: URLClassLoader child = new URLClassLoader( new URL[] {myJar.toURI().toURL()}, this.getClass().getClassLoader() ); Class … Read more

Dealing with “Xerces hell” in Java/Maven?

There are 2.11.0 JARs (and source JARs!) of Xerces in Maven Central since 20th February 2013! See Xerces in Maven Central. I wonder why they haven’t resolved https://issues.apache.org/jira/browse/XERCESJ-1454… I’ve used: <dependency> <groupId>xerces</groupId> <artifactId>xercesImpl</artifactId> <version>2.11.0</version> </dependency> and all dependencies have resolved fine – even proper xml-apis-1.4.01! And what’s most important (and what wasn’t obvious in the … Read more

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