classloader

How can I list all classes loaded in a specific class loader

情到浓时终转凉″ 提交于 2019-11-26 05:29:46
问题 For debug reasons, and curiosity, I wish to list all classes loaded to a specific class loader. Seeing as most methods of a class loader are protected, what is the best way to accomplish what I want? Thanks! 回答1: Instrumentation.getInitiatedClasses(ClassLoader) may do what you want. According to the docs: Returns an array of all classes for which loader is an initiating loader. I'm not sure what "initiating loader" means though. If that does not give the right result try using the

Java - how to load different versions of the same class?

我的梦境 提交于 2019-11-26 04:41:58
问题 I have read a lot about Java classloaders, but so far I have failed to find an answer for this simple question: I have two versions of com.abc.Hello.class in jars v1.jar and v2.jar . I want to use both in my application. What is the simplest way of doing this ? I don\'t expect to be that simple, but something along these lines would be awesome : Classloader myClassLoader = [magic that includes v1.jar and ignores v2.jar] Hello hello = myclassLoader.load[com.abc.Hello] And in a different class

Load properties file in JAR?

馋奶兔 提交于 2019-11-26 04:38:22
问题 I\'m having trouble when one of the jars that my web app depends on tries to load a properties file from within the jar. Here is the code in the jar. static { Properties props = new Properties(); try { props.load(ClassLoader.getSystemResourceAsStream(\"someProps.properties\")); } catch (IOException e) { e.printStackTrace(); } someProperty = props.getProperty(\"someKey\"); } The properties file is in my \"src/main/resources\" directory of the Maven project. When I run this code from my junit

How do I put all required JAR files in a library folder inside the final JAR file with Maven?

假装没事ソ 提交于 2019-11-26 04:36:57
问题 I am using Maven in my standalone application, and I want to package all the dependencies in my JAR file inside a library folder, as mentioned in one of the answers here: How can I create an executable JAR with dependencies using Maven? I want my final JAR file to have a library folder that contains the dependencies as JAR files, not like what the maven-shade-plugin that puts the dependencies in the form of folders like the Maven hierarchy in the .m2 folder. Well, actually the current

Order of loading jar files from lib directory

走远了吗. 提交于 2019-11-26 04:17:35
问题 Could anyone explain the order in which jar files are loaded from the lib directory within Tomcat? Is it alphabetically? Randomly? Or some other order? 回答1: It's all described in Tomcat's ClassLoading HOW-TO. It's not necessarily in alphabetic order. If you observed that behaviour, it should absolutely not be relied upon if you intend to keep your webapp portable across servers. For example, Tomcat 6 "coincidentally" orders it, but Tomcat 8 does not. Summarized, the loading order is as

Dealing with “Xerces hell” in Java/Maven?

◇◆丶佛笑我妖孽 提交于 2019-11-26 03:20:38
问题 In my office, the mere mention of the word Xerces is enough to incite murderous rage from developers. A cursory glance at the other Xerces questions on SO seem to indicate that almost all Maven users are \"touched\" by this problem at some point. Unfortunately, understanding the problem requires a bit of knowledge about the history of Xerces... History Xerces is the most widely used XML parser in the Java ecosystem. Almost every library or framework written in Java uses Xerces in some

this.getClass().getClassLoader().getResource(“…”) and NullPointerException

空扰寡人 提交于 2019-11-26 03:06:26
问题 I have created a minimal maven project with a single child module in eclipse helios. In the src/test/resources folder I have put a single file \"install.xml\". In the folder src/test/java I have created a single package with a single class that does: @Test public void doit() throws Exception { URL url = this.getClass().getClassLoader().getResource(\"install.xml\"); System.out.println(url.getPath()); } but when I run the code as a junit 4 unit test I just get a NullPointerException. This has

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?

拜拜、爱过 提交于 2019-11-26 02:29:53
问题 I would like to create a parent-last / child-first class loader, e.g. a class loader that will look for classes in the child class loder first, and only then delegate to it\'s parent ClassLoader to search for classes. Clarification: I know now that to get complete ClassLoading seperation I need to use something like a URLClassLoader passing null as it\'s parent, thanks to this answer to my previous question However the current question comes to help me resolve this issue: My code + dependent

Determine which JAR file a class is from

六月ゝ 毕业季﹏ 提交于 2019-11-26 02:27:31
问题 I am not in front of an IDE right now, just looking at the API specs. CodeSource src = MyClass.class.getProtectionDomain().getCodeSource(); if (src != null) { URL jar = src.getLocation(); } I want to determine which JAR file a class is from. Is this the way to do it? 回答1: Yes. It works for all classes except classes loaded by bootstrap classloader. The other way to determine is: Class klass = String.class; URL location = klass.getResource('/' + klass.getName().replace('.', '/') + ".class");

How to load a Java class dynamically on android/dalvik?

寵の児 提交于 2019-11-26 02:18:09
问题 I\'m wondering if and how one can load dex or class files dynamically in dalvik, some quick\'n\'dirty test function I wrote was this: public void testLoader() { InputStream in; int len; byte[] data = new byte[2048]; try { in = context.getAssets().open(\"f.dex\"); len = in.read(data); in.close(); DexFile d; Class c = defineClass(\"net.webvm.FooImpl\", data, 0, len); Foo foo = (Foo)c.newInstance(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch