classloader

Using the ClassLoader method to retrieve all resources under classes as Input Streams

天涯浪子 提交于 2019-12-04 04:00:58
问题 My problem is one that you would think is quite common, but I haven't so far managed to find a solution. Building a Java web app under Tomcat 5.5 (although a requirement is that it can be deployed anywhere, like under a WebLogic environment, hence the loading resources as streams requirement). Good practice dictates that resource files are placed under WEB-INF/classes and loaded using the ClassLoader's getResourceAsStream() method. All well and good when you know the name of the resource you

Java EE: Proxy cannot be cast to Local Interface, maybe classloading issue?

ε祈祈猫儿з 提交于 2019-12-04 04:00:52
问题 I'm currently "rearranging" my Java EE application, which consists of three components: MyAppInterface: mostly JPA- and JAXB-annotated POJOs, also some EJB Local Interfaces MyAppServer: JPA Facades, EJBs, Jersey resources MyAppWeb: GWT frontend, communicates with MyAppServer via HTTP/REST via loadbalancer Both MyAppServer and MyAppWeb use the classes defined in MyAppInterface; MyAppServer "exports" some of its EJBs via local interfaces in MyAppInterface. MyAppInterface is kind of the API, it

Android: How to dynamically load classes from a JAR file?

久未见 提交于 2019-12-04 03:58:51
问题 I'm trying to dynamically load a class at runtime on the Android platform. The class is contained within a separate library JAR file, but packaged with the APK (as per the new library mechanism in the SDK). When using Class.forname method, I received a class not found exception. I've seen some discussion around the DexClassLoader method, but I can't find a good example of how it's used (and whether it's the best way to go about it - it seems a lot more complicated than the forname approach!).

Copy java object/class from one classloader to another classloader

你离开我真会死。 提交于 2019-12-04 03:37:36
Hi is there a way to copy one class loaded context (atrributes etc) from one classloader (for instance a 'made' class Point) to another classloader? Making clear, Example: I have an object Point on CL 1. Now running on another CL2, I want to creat this object in CL 3. Some obj: class Point { int x; int y; public Point() {} //getters and setters Scenery: ... class CL2 { // Running on CL 2 ... // Point obj from CL 1 Object point = gotFromCL1(); // Want to create the object on Cl2 Object pointCL2 = point.conversion(); But I can't use sun.reflection (not available) and serialization doesn't work

Classloader resource paths are always absolute?

蓝咒 提交于 2019-12-04 03:26:09
问题 In a popular answer regarding the difference between class loading methods, Jon Skeet has stated, Classloader resource paths are always deemed to be absolute. An even more popular answer affirms this statement with an example. ClassLoader.getResourceAsStream(path) will consider all paths to be absolute paths. So calling String.getClassLoader().getResourceAsString("myfile.txt") and String.getClassLoader().getResourceAsString("/myfile.txt") will both look for a file in your classpath at the

Instantiate Java lambda function by name

喜夏-厌秋 提交于 2019-12-04 02:45:33
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(String[] args) throws Exception { { // Attempt with a static method as lambda Consumer<String> lambda =

scala classloaders confusion

这一生的挚爱 提交于 2019-12-04 02:41:00
Please consider the following test program (using scala 2.9.0.1) object test { def main(args:Array[String]) = { println(ClassLoader.getSystemClassLoader.getResource("toto")) println(this.getClass.getClassLoader.getResource("toto")) println(classOf[Object].getClassLoader) } } I compile it and run it with "-cp /tmp" containing a file "toto", and I get the following output: null file:/tmp/toto null => the system classloader does not contain the classpath => the Object class has no classloader! Am I missing something there or is it a (big) bug in scala?! Thanks, Arjun The second null is explained

Loading two classes in different JARs

荒凉一梦 提交于 2019-12-04 01:40:56
问题 I got two classes with the same package in different JARs. Until the previous version, both classes were identical, so i had no issues in loading them. Now, one of them has a new method added and if I want to access it, not only should I import the class with that package, i also need to make sure the jar with the correct class comes first in the classpath. i.e. javac -classpath "%classpath%;a.jar;b.jar" MyClasses.. where a.jar has the class with my new method. Now, how do i ensure this when

What is the difference between getResourceAsStream with and without getClassLoader?

泄露秘密 提交于 2019-12-04 01:33:24
I'd like to know the difference between the following two: MyClass.class.getClassLoader().getResourceAsStream("path/to/my/properties"); and MyClass.class.getResourceAsStream("path/to/my/properties"); Thank you. From the Javadoc for Class.getResourceAsStream() : This method delegates to this object's class loader. Before delegation, an absolute resource name is constructed from the given resource name using this algorithm: If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'. Otherwise, the absolute name is of the

Converting a given class (let's say, java.lang.Object) to a byte array. Is it possible?

£可爱£侵袭症+ 提交于 2019-12-04 01:32:39
问题 Given that class loaders accept to take as input a byte array of a given class, returning a Class<?> , I wonder whether it is possible to do the reverse, that is, to pass a Class<?> and get its byte array? Keep in mind that I'm not talking about serialization! 回答1: You can use ClassLoader.getResourceAsInputStream() however it is not guaranteed this will be the same bytes as that which was loaded. I don't believe the actual bytes loaded are stored anywhere. 回答2: No standard way I know of. But