classloader

Is JDK ClassLoader.getResourceAsStream broken? (unclosed resources)

自作多情 提交于 2019-12-06 03:17:19
问题 I will try to prove that ClassLoader.getResourceAsStream() is opening two InputStreams , closing none of it and returning only one to client. Is my logic correct? JDK sources are picked from jdk1.8.0_25 I've get into unclosed resources problem using Spring ClassPathResource in interval (original question), that is using ClassLoader.getResourceAsStream to get InputStream to a properties file. After investigation, I found that classLoader.getResourceAsStream is getting an URL by URL url =

Is it possible load all properties files contained in a package dynamically? i.e. MyClass.class.getResource('*.properties');

橙三吉。 提交于 2019-12-06 03:08:41
问题 I am familiar with obtaining the contents of a properties file given the name of the file, and obviously MyClass.class.getResource('*.properties') will not work, but how can I obtain a list of ALL the properties files located in the same package as my class? 回答1: You can do these sort of things with Spring . See 4. Resources, particurlarely (or notabily ? ) (or principalementely ? ) (or mainly ? ) at 4.7.2 Wildcards in application context constructor resource paths. 回答2: Assuming that it's

Dynamic class reloading works only in debug mode, why/ how it really works?

情到浓时终转凉″ 提交于 2019-12-06 03:01:39
I have a tricky question. My java program is doing in a loop such things: loop: read external file with compiled java class into byte[] array. create a new instance of my own classloader. setbytes from the readed file to this instance of classloader. using created classloader create a new instance of object of the class from external file. call any method of the created object. And where the problem is. When I run this program in debug mode it behaves as I expect, so if external file changed classloader loads new version of class and executes new version (if file didn't change it loads old

How to load a velocity template into an EJB to be used as a mail template

99封情书 提交于 2019-12-06 02:22:18
I have a Java EE 6 application in which I'd like to use velocity to generate mails from a template. I have a @Named bean which is responsible for loading and filling a particular template. The project is a web application, so I placed my templates into WEB-INF/classes (which btw seems to be rather ugly, but I didn't find a more elegant solution by now) and used the ClasspathResourceLoader to access the files. The configuration is as follows: Properties props = new Properties(); props.setProperty("resource.loader", "class"); props.setProperty("resource.loader.class", "org.apache.velocity

Why does Class.getPackage return the same Package for classes from different packages?

纵然是瞬间 提交于 2019-12-06 02:05:09
问题 I make a new ClassLoader and make it define a new Class, which means that new class should be in a new namespace, which it is, AFAIK. The strange thing is, when I call Class.getPackage on the new class, it returns the exact same object as returned by calling getPackage on any other class in my main namespace. According to the JVM spec: The runtime package of a class or interface is determined by the package name and defining class loader of the class or interface. So in other words, if you

Create Classloader with only classes inside specific folder

眉间皱痕 提交于 2019-12-06 00:29:03
I want to load specific jars in ScriptEngineManager using specific ClassLoader This constructor loads the implementations of ScriptEngineFactory visible to the given ClassLoader using the service provider mechanism. The problem when I tried to create Classloader File file = new File("c:\\myclasses\\"); try { // Convert File to a URL URL url = file.toURI().toURL(); // file:/c:/myclasses/ URL[] urls = new URL[]{url}; // Create a new class loader with the directory ClassLoader cl = new URLClassLoader(urls) And I'm getting also class that weren't in class for example Spring Class cls1 = cl

Where and when is the instance of PathClassLoader created in android source code?

一笑奈何 提交于 2019-12-05 20:41:22
When I was studying android source code, I noticed that the general class loader in app is an instance of PathClassLoader , and there is two constructors in this class. One is like: public PathClassLoader(String dexPath, ClassLoader parent) { super(dexPath, null, null, parent); } and the other is like: public PathClassLoader(String dexPath, String libraryPath, ClassLoader parent) { super(dexPath, null, libraryPath, parent); } But I cannot find the call of the second constructor in the source code during the procedure of app launching. So where does the value of libraryPath param come from? As

Different behavior of ClassLoader.getSystemClassLoader().getResource() in servlet container and test environment

社会主义新天地 提交于 2019-12-05 20:07:49
I have a web application my requirement is to read some files and process it and persist the file content in database when the application starts. class MyUtil{ /** *Read the files */ public static void readFiles(){ File file = new File(ClassLoader.getSystemClassLoader().getResource("MyFile").toURI()); //NullPointerException // ClassLoader.getSystemClassLoader().getResource("MyFile") is giving null in servlet.init() method. if (file.isDirectory()) { //Read all the files and persist. } } } MyFile folder/dir is available in class path. When MyUtil.readFiles() is called in JUnit test case it

Instantiate Java lambda function by name

六月ゝ 毕业季﹏ 提交于 2019-12-05 19:50:19
问题 I would like to create a lambda function in Java 8, get it's classname and then later instantiate the function again from its classname. This is what I try: import java.util.function.Consumer; public class SimpleLambda { public static void call(String aLambdaClassName, String aArg) throws Exception { Class<Consumer<String>> lClass = (Class<Consumer<String>>) Class.forName(aLambdaClassName); Consumer<String> newlamba = lClass.newInstance(); newlamba.accept(aArg); } public static void main

Can java string literals be garbage collected?. If Yes, how to prove it?

主宰稳场 提交于 2019-12-05 19:40:41
Can java String literals like "abc" be garbage collected?. If yes, how can we programatially prove that they are GCed? Yes, post Java7, String literals can be garbage collected if the class loader which loaded it gets garbage collected and there are no references to the string literal. Note : In Java -8, you will have to call GC twice in order to ensure that ClassLoaders get GCed (Metaspace.. pfff..Using a different GC won't help). Case -1 : //ClassLoaders don't get GCed. Code : import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; // main class class