Questions tagged [classloader]

A class loader is an object that is responsible for loading classes in Java.

A class loader is an object that is responsible for loading classes in Java.

Given the binary name of a class, a class loader should attempt to locate or generate data that constitutes a definition for the class. A typical strategy is to transform the name into a file name and then read a "class file" of that name from a file system. A class loader may also chose to delegate to another class loader. It may do this before or after doing a normal lookup.

Every Class object contains a reference to the class loader that defined it. This class loader is used by the runtime for linking to other classes referenced by this class. Together with the name of the class the defining class loader uniquely identifies a class. It's therefore possible to have several classes in with the same name in Java as long as they have a different defining class loader.

4126 questions
14
votes
2 answers

Get bytecode from loaded class

Suppose in my JVM I have a loaded class Class myClass. Is there a reliable way to ask the JVM for the bytecode contents of the .class? I.e. something like this: byte[] getClassBytecode(Class myClass) { return /* the contents of the…
CAFxX
  • 28,060
  • 6
  • 41
  • 66
14
votes
8 answers

this.getClass().getResource("").getPath() returns an incorrect path

I am currently making a small simple Java program for my Computer Science Final, which needs to get the path of the current running class. The class files are in the C:\2013\game\ folder. To get this path, I call this code segment in my main class…
user2465495
  • 149
  • 1
  • 1
  • 3
14
votes
2 answers

Java class loader tutorial

Please provide a good, thorough tutorial about Java class loading, focusing on how to extend that mechanism and how to actually work with the extension.
Yaneeve
  • 4,751
  • 10
  • 49
  • 87
14
votes
2 answers

java.lang.IllegalAccessError: tried to access field ConcreteEntity.instance from class Entity

java.lang.IllegalAccessError: tried to access field ConcreteEntity.instance from class Entity Ok so here is the deal. I am trying to access ConcreteEntity.instance which is a field with the access type default that exists inside the default…
Justin
  • 436
  • 1
  • 3
  • 9
14
votes
4 answers

Can Java Classloader's rewrite the bytecode of (only their copy of) System classes?

So I have a classloader (MyClassLoader) that maintains a set of "special" classes in memory. These special classes are dynamically compiled and stored in a byte array inside MyClassLoader. When the MyClassLoader is asked for a class, it first checks…
Li Haoyi
  • 15,330
  • 17
  • 80
  • 137
14
votes
2 answers

is it possible to dynamically load a Activity class from a jar library on the sdCard & actually use it?

I have 1 .jar file on sdCard (i used dx tool to compile a usual .jar to dex.. otherwise android will complain at runtime) The class that i want to use is actually a Activity & by "use" i mean Inflate! A) i've noticed that if i load class like this,…
pulancheck1988
  • 2,024
  • 3
  • 28
  • 46
14
votes
2 answers

Loading files with ClassLoader

This problem has been bugging me for a while. I have to load a couple files in my java app, and the only way I got working so far looks like this: URL hsURL; if(System.getProperty("os.name").toLowerCase().contains("windows")) { hsURL = new…
twolfe18
  • 2,228
  • 4
  • 24
  • 25
13
votes
3 answers

java.lang.LinkageError: ClassCastException

I do experience a really annoying problem with TestNG and RESTeasy. I do have a class that runs several tests against an API class which uses the RESTeasy framework to expose itself. However if I let the test run with maven (mvn test), then I get…
rit
  • 2,308
  • 3
  • 19
  • 27
13
votes
4 answers

Multiple instances of static variables

I'm experimenting with using different classloaders to load a particular class, and see if the static variables in that class can have different instances. Basically, I'm trying to write code for what Stephen C has mentioned in this answer. Here…
AbdullahC
  • 6,649
  • 3
  • 27
  • 43
13
votes
4 answers

Loading Spring context with specific classloader

How can I go about loading a Spring context with my own ClassLoader instance?
Synesso
  • 37,610
  • 35
  • 136
  • 207
13
votes
5 answers

ClassLoader Leak - Are they worth solving?

ClassLoader leaks usually result in java.lang.OutOfMemoryError: PermGen. In the instance of working on application servers you may see this as a result of many redeploys of a common application. The explanation and possible resolutions to this…
John Vint
  • 39,695
  • 7
  • 78
  • 108
13
votes
1 answer

How can I isolate my Jenkins pipeline Groovy shared library classloader?

I have a Groovy library made available as a Global shared library: package com.example @Grab(group="org.apache.httpcomponents", module="httpclient", version="[4.5.3,)") import org.apache.http.HttpHost import…
Patrick
  • 5,714
  • 4
  • 31
  • 36
13
votes
1 answer

How to set my custom class loader to be the default?

I'm trying to practice myself with custom class loaders, and I've some questions. Is there a way to indicate the JVM to use my custom class loader globally? For example, I wrote small app running under Tomcat 6. The servlet is managed by the…
Seffy
  • 1,045
  • 1
  • 13
  • 27
13
votes
8 answers

Java or any other language: Which method/class invoked mine?

I would like to write a code internal to my method that print which method/class has invoked it. (My assumption is that I can't change anything but my method..) How about other programming languages? EDIT: Thanks guys, how about JavaScript? python?…
DuduAlul
  • 6,313
  • 7
  • 39
  • 63
13
votes
5 answers

Does jvm load all the classes mentioned by the classpath?

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?
hsingh
  • 661
  • 5
  • 26