classloader

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

早过忘川 提交于 2019-11-26 11:11:06
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 (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch

Difference between Loading a class using ClassLoader and Class.forName

假装没事ソ 提交于 2019-11-26 10:23:40
问题 Below are 2 code snippets The first one uses ClassLoader class to load a specified class ClassLoader cls = ClassLoader.getSystemClassLoader(); Class someClass = cls.loadClass(\"TargetClass\"); The second one uses Class.forName() to load a specified class Class cls = Class.forName(\"TargetClass\"); What is the difference between the above said approaches. Which one serves for which purpose? 回答1: Quick answer (without code samples) With the explicit ClassLoader cls = <a ClassLoader>; approach

What does JVM flag CMSClassUnloadingEnabled actually do?

偶尔善良 提交于 2019-11-26 09:53:10
I cannot for the life of me find a definition of what the Java VM flag CMSClassUnloadingEnabled actually does, other than some very fuzzy high-level definitions such as "gets rid of your PermGen problems" ( which it doesn't , btw). I have looked on Sun's/Oracle's site, and even the options list doesn't actually say what it does. Based upon the name of the flag, I'm guessing that the CMS Garbage Collector doesn't by default unload classes, and this flag turns it on - but I can't be sure. Update This answer is relevant for Java 5-7, Java 8 has this fixed: https://blogs.oracle.com/poonam/about-g1

Java Enums: Two enum types, each containing references to each other?

狂风中的少年 提交于 2019-11-26 09:41:14
问题 Is there a way to get around the class-loading issues caused by having two enums that reference each other? I have two sets of enumerations, Foo and Bar, defined like so: public class EnumTest { public enum Foo { A(Bar.Alpha), B(Bar.Delta), C(Bar.Alpha); private Foo(Bar b) { this.b = b; } public final Bar b; } public enum Bar { Alpha(Foo.A), Beta(Foo.C), Delta(Foo.C); private Bar(Foo f) { this.f = f; } public final Foo f; } public static void main (String[] args) { for (Foo f: Foo.values()) {

Class.forName() vs ClassLoader.loadClass() - which to use for dynamic loading? [duplicate]

会有一股神秘感。 提交于 2019-11-26 08:43:49
问题 This question already has an answer here: Difference between Loading a class using ClassLoader and Class.forName 9 answers When dynamically loading a class, when is it appropriate to use Class.forName(\"SomeClass\"); and when should I use ClassLoader.getSystemClassLoader().loadClass(\"SomeClass\"); Or, are they two ways of doing the same thing? 回答1: They are quite different! As stated in the documentation for Class.forName(String), Returns the Class object associated with the class or

In Java, is it possible to know whether a class has already been loaded?

ε祈祈猫儿з 提交于 2019-11-26 07:29:15
问题 Is it possible to know whether a Java class has been loaded, without attempting to load it? Class.forName attempts to load the class, but I don\'t want this side effect. Is there another way? (I don\'t want to override the class loader. I\'m looking for a relatively simple method.) 回答1: (Thanks to Aleksi) This code: public class TestLoaded { public static void main(String[] args) throws Exception { java.lang.reflect.Method m = ClassLoader.class.getDeclaredMethod("findLoadedClass", new Class[]

Strange behavior of Class.getResource() and ClassLoader.getResource() in executable jar

大城市里の小女人 提交于 2019-11-26 07:28:25
问题 I understand from What is the difference between Class.getResource() and ClassLoader.getResource()? and from own code, that getClass().getResource(\"/path/image.png\") is identical to getClass().getClassLoader().getResource(\"path/image.png\") The posting Cannot read an image in jar file shows an issue where using getClass().getClassLoader().getResource(\"path/image.png\") in an executable jar file returns null, while getClass().getResource(\"/path/image.png\") returns the correct URL. Since

How to use ClassLoader.getResources() correctly? [duplicate]

◇◆丶佛笑我妖孽 提交于 2019-11-26 06:28:45
问题 This question already has answers here : How to list the files inside a JAR file? (15 answers) Closed 3 years ago . How can I use ClassLoader.getResources() to find recursivly resources from my classpath? E.g. finding all resources in the META-INF \"directory\": Imagine something like getClass().getClassLoader().getResources(\"META-INF\") Unfortunately, this does only retrieve an URL to exactly this \"directory\". all resources named bla.xml (recursivly) getClass().getClassLoader()

How to deal with LinkageErrors in Java?

*爱你&永不变心* 提交于 2019-11-26 06:26:12
问题 Developing a heavily XML-based Java-application, I recently encountered an interesting problem on Ubuntu Linux. My application, using the Java Plugin Framework, appears unable to convert a dom4j-created XML document to Batik\'s implementation of the SVG specification. On the console, I learn that an error occurs: Exception in thread \"AWT-EventQueue-0\" java.lang.LinkageError: loader constraint violation in interface itable initialization: when resolving method \"org.apache.batik.dom.svg

How to get names of classes inside a jar file?

≡放荡痞女 提交于 2019-11-26 05:57:33
问题 I have a JAR file and I need to get the name of all classes inside this JAR file. How can I do that? I googled it and saw something about JarFile or Java ClassLoader but I have no idea how to do it. 回答1: Unfortunately, Java doesn't provide an easy way to list classes in the "native" JRE. That leaves you with a couple of options: (a) for any given JAR file, you can list the entries inside that JAR file, find the .class files, and then determine which Java class each .class file represents; or