classloader

Does system classloader load all classes in classpath even if they're not actually used?

荒凉一梦 提交于 2019-12-20 02:54:45
问题 I'm using JDK 1.6 to run a small application. However I set a very massive classpath which includes a lot of classes. When I run the application will all classes in the classloader been loaded even if they're not actually used in my application? If not, how to force the classloader do so, and if yes, how to avoid it? Thanks! E.g.,I'm using ant 1.7 to run my application. Best Regards, Robert Ji 回答1: No, The ClassLoader loads the class when the class is needed in memory. It doesn't load all

Custom URLClassLoader, NoClassDefFoundError when run

匆匆过客 提交于 2019-12-20 02:44:28
问题 I've created my own URLClassLoader , and set it as the system classloader via java.system.class.loader . It's initialized and everything, but the classes I'm trying to load aren't found. Here's the URLClassLoader : public class LibraryLoader extends URLClassLoader { public LibraryLoader(ClassLoader classLoader) { super(new URL[0], classLoader); } synchronized public void addJarToClasspath(String jarName) throws MalformedURLException, ClassNotFoundException { File filePath = new File(jarName);

Classpath resources inside one-jar packaged via sbt

試著忘記壹切 提交于 2019-12-20 01:10:06
问题 I have a project that is build using SBT which packages a single jar using the one-jar plugin. That project contains a bunch of json files in src/main/resources/fixture which I used to access via new java.io.File(App.getClass.getResource("/fixture").getFile Unfortunately this doesn't work any longer since no Resource is returned. I think one-jar uses a special classloading mechanism? Whats the best way to solve this? 回答1: I think one-jar uses a special classloading mechanism? Yes, this must

Tomcat 8 classloading - difference between JAR in [WEB-INF/lib] and [tomcat/lib]

社会主义新天地 提交于 2019-12-19 22:00:16
问题 It says here that the common classloader is visible to all web application. So what is the difference between having a JAR file within the WEB-INF/lib folder of my war application and having the same JAR file within Tomcat's lib folder? This JAR is a provider for a Java SPI that I created. When I have it under WEB-INF/lib , everything works perfectly. But if I try to put this JAR under Tomcat's lib folder (or under shared/lib after configuring it in catalina.properties ) I get errors. The

Loading JDBC Driver at Runtime

点点圈 提交于 2019-12-19 19:48:10
问题 I'm using the following code to load a driver class: public class DriverLoader extends URLClassLoader { private DriverLoader(URL[] urls) { super(urls); File driverFolder = new File("driver"); File[] files = driverFolder.listFiles(); for (File file : files) { try { addURL(file.toURI().toURL()); } catch (MalformedURLException e) { } } } private static DriverLoader driverLoader; public static void load(String driverClassName) throws ClassNotFoundException { try { Class.forName(driverClassName);

Classes missing if application runs for a long time

我与影子孤独终老i 提交于 2019-12-19 17:48:12
问题 I have a funny problem - if my application runs for a long time (> 20h), then sometimes I get NoClassDefFound error - seems like JVM decided that the class is not going to be used anyway and GCd it. To be a bit more specific, here's an example case: object ErrorHandler extends PartialFunction[Throwable,Unit] { def isDefinedAt(t: Throwable) = true def apply(e: Throwable) =e match { // ... handle errors } } // somewhere else in the code... try { // ... long running code, can take more than 20

Define custom system classloader

拟墨画扇 提交于 2019-12-19 11:18:29
问题 I'm trying to tell the JVM to use my custom ClassLoader as default ClassLoader This is the VM argument i use to pick my class: -Djava.system.class.loader=JarClassLoader and this is the error i get Error occurred during initialization of VM java.lang.Error: java.lang.NoSuchMethodException: JarClassLoader.<init>(java.lang.ClassLoader) at java.lang.ClassLoader.initSystemClassLoader(Unknown Source) at java.lang.ClassLoader.getSystemClassLoader(Unknown Source) Caused by: java.lang

Applet class loader cannot find a class in the applet's jar

牧云@^-^@ 提交于 2019-12-19 08:30:26
问题 I started to ask this question and then figured out the answer before submitting it. I've decided to post the question anyway so that other people who run into the same problem will be able to learn from my mistakes. I'm having a problem with an applet (a JApplet actually) unable to instantiate another class which is included in the same jar as the applet. The exception I'm seeing on the Java console is: Exception in thread "thread applet-com.company.program.cm.hmi.MediatorApplet-1" java.lang

How does one access a method from an external jar at runtime?

半腔热情 提交于 2019-12-19 07:27:37
问题 This is a continuation of the question posted in: How to load a jar file at runtime I am uncertain as to how to continue to the method invocation level. From my understanding, from the clazz object, I would used getMethod or getDeclaredMethod to get a Method object from which I would call invoke. Of course, invoke requires an instance. Would that then be what is called doRun in the example code? Do I need to perform the doRun.run() method call even though I want to execute a different method

why doesn't this code throw NullPointerException

本秂侑毒 提交于 2019-12-19 05:19:36
问题 I was just discussing about calling static methods using class name with my friend and tried out this code and expected it to throw NPE at runtime.but as it turn out it dint. i just want to understand the execution order. public class One { public static void method() { System.out.println("in static one"); } } public class Two { static One o; public static void main(String[] args) { o.method(); // expected NPE here, as o is null } } I know that static methods should be invoked with their