classloader

What is the use of Custom Class Loader

懵懂的女人 提交于 2019-11-30 10:42:45
问题 Recently I came accross the java custom class loader api. I found one use over here, kamranzafar's blog I am a bit new to the class loader concept. Can any one explain in detail, what are the different scenarios where we may need it or we should use it? 回答1: Custom class loaders are useful in larger architectures consisting of several module/applications. Here are the advantages of the custom class loader: Provides Modular architecture Allows to define multiple class loader allowing modular

Java: Difference between Class.forName and ClassLoader.loadClass

烈酒焚心 提交于 2019-11-30 10:14:17
问题 Recently came across some code that got me thinking. What's the difference between: Class theClass = Class.forName("SomeImpl"); SomeImpl impl = (SomeImpl)theClass.newInstance(); and: Class theClass = ClassLoader.loadClass("SomeImpl"); SomeImpl impl = (SomeImpl)theClass.newInstance(); Are they synonymous? Is one preferable to the other in certain circumstances? What are the do's and dont's to using these two methods? Thanks in advance. 回答1: Class.forName() will always use the ClassLoader of

OSGi, Java Modularity and Jigsaw

▼魔方 西西 提交于 2019-11-30 10:10:01
问题 So as of yesterday morning I hadn't a clue as to what OSGi even was. OSGi was just some buzzword that I kept seeing cropping up over and over again, and so I finally set aside some time to brush up on it. It actually seems like pretty cool stuff, so I'd like to start off by stating (for the record) that I'm not anti-OSGi in any respect, nor is this is some "OSGi-bashing" question. At the end of the day, it seems that OSGi has - essentially - addressed JSR 277 on Java Modularity, which

Why is the setContextClassLoader() method placed on Thread?

半腔热情 提交于 2019-11-30 09:34:48
Why is the setContextClassLoader() method placed on Thread ? What different thread have different classloaders? The question is what if I extended a ClassLoader , loaded there some new classes. to the my custom classloader. Now , I want it to be the context classloader , so I call the method Thread.currentThread().setContextClassLoader(loader) . Are these new classes awailable only in the context of the current thread ? Or how does it work? Thanks The Context class loader is the class loader that the thread will use to find classes. You primarily care about this when you are writing an

Load Java-Byte-Code at Runtime

放肆的年华 提交于 2019-11-30 09:18:06
I got some java-byte-code (so compiled java-source) which is generated in my program. Now I want to load this byte-code into the currently running Java-VM and run a specific function. I'm not sure how to accomplish this, I digged a little bit into the Java Classloaders but found no straight way. I found a solution which takes a class-file on the harddisk, but the bytecode I got is in a Byte-Array and I dont want to write it down to the disk but use it directly instead. Thanks! Nikolaus Gradwohl you need to write a custom class loader that overloads the findClass method public Class findClass

Tomcat Java Servlet - Initialize Class on Application Startup

試著忘記壹切 提交于 2019-11-30 09:07:26
I have a class that takes a bit of time to start up (makes some JNI calls and what not), so it is not feasable to initialize this class everytime a page loads. Is it possible to initialize this class on application startup, then access its methods as pages are being served up? For Example: I have MyClass. When the application (tomcat) starts up I would like it to initialze my calss as follows: MyClass myClassInstance = new MyClass("arg1", "arg2"); Then when a page is called, say /testpage, I would like to make calls to myClassInstance: import java.io.*; import javax.servlet.*; import javax

Java ClassLoader change

一世执手 提交于 2019-11-30 09:02:44
问题 I have some class A : public class A { public A(String str) { System.out.println("Create A instance: " + str); } public void methodA() { System.out.println("#methodA1()"); } } And my class loader implementation: public class MyClassLoader extends ClassLoader { public MyClassLoader() { super(); } @Override public synchronized Class<?> loadClass(String name) throws ClassNotFoundException { System.out.println("Load: " + name); return super.loadClass(name); } } And now I try to change default

Unit test using the Reflections google library fails only when executed by Maven

纵然是瞬间 提交于 2019-11-30 08:34:42
问题 I am using the Google Reflections library for querying certain resources in the classpath. Those resources are located in the same location than the classes in my project. I wrote some unit tests that succeed when executed as a unit test in Eclipse, but when I try to execute them with Maven (with a maven install for example), they are not working as expected. After some debugging, apparently the problem is that when executed with Maven, the Reflections library cannot find the classpath url

Configure org.apache.log4j.ConsoleAppender with custom classloader

喜欢而已 提交于 2019-11-30 08:27:04
问题 I have a java class which creates a custom classloader based on javassist class loader on start up and then run the real program class. I'm getting the following error: log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to a "org.apache.log4j.Appender" variable. log4j:ERROR The class "org.apache.log4j.Appender" was loaded by log4j:ERROR [javassist.Loader@6f97b10a] whereas object of type log4j:ERROR "org.apache.log4j.ConsoleAppender" was loaded by [java.net

What for Sun JVM creates instances of sun.reflect.DelegatingClassLoader at runtime?

你离开我真会死。 提交于 2019-11-30 08:20:20
While analyzing a heap dump using jhat I have observed many instances of DelegatingClassLoader created although they were not called explicitly in code. I expect this to be some sort of reflection optimization mechanism. Does anybody know the details? Yes, it is probably a reflection optimisation. On the Sun JVM, reflective access to properties and methods is initially performed by calling through JNI into the JVM implementation. If the JVM notices that a method or field is being accessed by reflection a lot, it will generate bytecode to do the same thing -- a mechanism that it calls