classloader

Java: Load class from string

孤街浪徒 提交于 2019-11-29 08:53:09
I know this has probably something to do with class loaders, however I couldn't find an example (it might be I'm google-ing for the wrong keywords. I am trying to load a class (or a method) form a string. The string doesn't contain the name of a class, but the code for a class, e.g. class MyClass implements IMath { public int add(int x, int y) { return x + y; } } and then do something like this: String s = "class MyClass implements IMath { public int add(int x, int y) { return x + y; }}"; IMath loadedClass = someThing.loadAndInitialize(string); int result = loadedClass.add(5,6); Now obviously,

How to make the jvm load my java.lang.String instead of the one in rt.jar

ぃ、小莉子 提交于 2019-11-29 07:41:21
I studied java classloader rencently. Now I want to write a class that has the same package name and class name as one of class in rt.jar. For example, write a java.lang.String class by myself, and how to break the parents delegation model to make the jvm load my java.lang.String instead of the one in rt.jar. Re-edit Thx, tried. And ↓↓↓ D:\>java -verbose -Xbootclasspath/p:D:/myrt.jar -jar exe.jar [Opened D:\myrt.jar] [Opened C:\java\jre\lib\rt.jar] [Loaded java.lang.Object from C:\java\jre\lib\rt.jar] [Loaded java.lang.String from D:\myrt.jar] [Loaded java.io.Serializable from C:\java\jre\lib

Classloader behaviour on Tomcat with multiple applications

亡梦爱人 提交于 2019-11-29 07:08:16
问题 On a Tomcat 5.5 server, I put a class in the system classpath (and modify catalina.bat to pick it), or if I put class in the shared lib directory. Now if I have two different applications using the same class which do not have the class in their WEB-INF lib/classes directories, they use the same instance of the class. I understand the concept that a classloader will delegate to it's parent classloader for finding a class if it can't find it, so in this case, since the class is not present in

XPath class resolution in JBoss5

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-29 06:52:24
I'm having a hard time figuring out where the problem is coming from, so I'm posting this in the hopes that others might have found something similar to this elsewhere and are kind enough to share their insight. I'm using a JBoss 5.0.1.GA application server running on top of a Sun Java 1.6.0-13 JDK . For the WAR file in the generated Web Service, I use a Axis2 1.4 WS engine whose JAR files are inserted by Eclipse Galileo into the project's WEB-INF/lib directory when creating the Webservice from the given "worker" class in the Dynamic Web Project. The relevant code snippet follows: String sUrl

Configure org.apache.log4j.ConsoleAppender with custom classloader

江枫思渺然 提交于 2019-11-29 06:42:59
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.URLClassLoader@5b414a8d]. log4j:ERROR Could not instantiate appender named "stdout". The problem is related to the

Explanation of how classloader loads static variables

主宰稳场 提交于 2019-11-29 06:37:59
Ok so this is a newbie question on java, but i can't seem to get my head around it. I have the following code inside my class private static final String [] LIST_CODE = gerarListCode(); private static final int [][] LIST_INTEGER = new int [][] { {947,947}, {110,103}, {947,958}, {110,120}, {947,954}, {103,107}, {947,967}, {110,99,104}}; private static String [] gerarListCode() { String [] listCode = new String [LIST_INTEGER.length]; for (int i=0 ; i<LIST_INTEGER.length ; i++) { //do some stuff } return listaUnicode; } This code is giving me a initialization exception due to a

Replace content of some methods at runtime

﹥>﹥吖頭↗ 提交于 2019-11-29 05:29:58
I would like to replace the content of some methods at runtime. I know I can use javassist for this but it does not work because the classes I would like to enhance are already loaded by the system classLoader . How can I do, to replace the content of a method at runtime ? Should I try to unload the class ? How can I do that ? I saw it was possible but I could not figure out how to do it. If possible, I would like to avoid using an external lib for this, I would like to code it my-self. More information: - The class I would like to enhance is contained in a framework (in a jar file) - My code

JBoss Scoped Class Loading

☆樱花仙子☆ 提交于 2019-11-29 05:16:38
I want to use the latest hibernate version inside the ear without upgrading the jars on the server. I am following the instructions given here - http://jaitechwriteups.blogspot.com/2008/08/how-to-upgrade-hibernate-in-jboss.html . However the problem now is the application is not taking the jboss-local-jdbc.rar sitting in the deploy folder. 2009-07-21 09:01:50,347 INFO [org.jboss.system.ServiceConfigurator] Problem configuring service jboss.jca:service=DataSourceBinding,name=MockDS org.jboss.deployment.DeploymentException: Exception setting attribute ConnectionManager = jboss.jca:service

How are object dependencies between static blocks resolved?

血红的双手。 提交于 2019-11-29 04:51:24
问题 I recently came across this at work. While I am not sure it is really a good idea, I don't understand how static blocks are handled by the compiler. Here is an example: Consider that you have classes A and B : public class A { public final static List<Integer> list; static { list = new ArrayList<>(); } } public class B { public final static int dependsOnA; static { dependsOnA = A.list.size(); } } And a main class that just reads B.dependsOnA . The static block in B is dependent of the one in

Does jvm load all the classes mentioned by the classpath?

送分小仙女□ 提交于 2019-11-29 04:05:53
When we invoke java command with -cp command then we provide some directories and jar files. Does jvm load all the classes mentioned by the classpath Or it is just a super set of all classes which jvm will look up to load when required? Does jvm load all the classes mentioned by the classpath Or it is just a super set of all classes which jvm will look up to load when required? JVM loads classes form the classpath on the need basis i.e. when a reference is found for the class, it is loaded. Also there is a hierarchy of class loaders in JVM, a class loaded by the parent class loader is used by