classloader

Classpath resources inside one-jar packaged via sbt

别等时光非礼了梦想. 提交于 2019-12-01 21:04:09
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? I think one-jar uses a special classloading mechanism? Yes, this must be true since there is no standardized way to load classes that packaged into dependency jar that is in turn

How to use ClassLoader.getResources() in jar file

三世轮回 提交于 2019-12-01 20:37:39
Problem statement: I have a jar file with a set of configuration files in a package mycompany/configuration/file/ . I don't know the file names. My intention is to load the file names at runtime from the jar file in which they are packaged and then use it in my application. As far as I understood: When I use the ClassLoader.getResources("mycompany/configuration/file/") I should be getting all the configuration files as URLs. However, this is what is happening: I get one URL object with URL like jar:file:/C:/myJarName.jar!mycompany/configuration/file/ Could you please let me know what I am

ClassLoader.getSystemClassLoader() vs obj.getClass().getClassLoader().getSystemClassLoader()

时光总嘲笑我的痴心妄想 提交于 2019-12-01 20:09:39
Are these exactly same: ClassLoader.getSystemClassLoader() // 1 vs: obj.getClass().getClassLoader().getSystemClassLoader() // 2 Person.class.getClassLoader().getSystemClassLoader() Is there a situation possible where they could yield different results? As per ClassLoader.getSystemClassLoader() javadoc this is normally the class loader used to start the application. The java.system.class.loader property can be used to override the returned class loader, however: The system property to override the system class loader is not examined until the VM is almost fully initialized. Code that executes

Find the ClassLoader loading a specific class

你说的曾经没有我的故事 提交于 2019-12-01 19:53:32
Is there a way to determine which ClassLoader loads a specific class? Or more specifically from where a specific class is loaded? I've a situation where an old db driver class is loaded. I'd like to find the file where the old driver is loaded from. My initial approach is to set a debug point on the ClassLoader.loadClass(..) method and stop the vm once the class is getting loaded to see which classloader is loading it. Unfortunately the loadClass method is called so often that its difficult to stop where the class is loaded. I'll try to set a breakpoint filter. There is, however, another

Classloader resource paths are always absolute?

一世执手 提交于 2019-12-01 19:16:37
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 following location: ./myfile.txt . Disregarding the fact that the example won't compile, consensus

Loading JDBC Driver at Runtime

ε祈祈猫儿з 提交于 2019-12-01 17:55:57
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); } catch (ClassNotFoundException ex) { if (driverLoader == null) { URL urls[] = {}; driverLoader = new

Classes missing if application runs for a long time

放肆的年华 提交于 2019-12-01 17:24:46
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 hours to complete } catch (ErrorHandler) And I get the following exception: Exception in thread "main"

Overriding default accessor method across different classloaders breaks polymorphism

点点圈 提交于 2019-12-01 16:58:23
I come across to a strange behavior while trying to override a method with default accessor (ex: void run() ). According to Java spec, a class can use or override default members of base class if classes belongs to the same package. Everything works correctly while all classes loaded from the same classloader. But if I try to load a subclass from separate classloader then polymorphism don't work. Here is sample: App.java: import java.net.*; import java.lang.reflect.Method; public class App { public static class Base { void run() { System.out.println("error"); } } public static class Inside

[tomcat7源码学习]通过ClassLoader测试动态加载Jar

百般思念 提交于 2019-12-01 16:32:38
ClassLoader测试动态加载Jar 1.写一个自己的 ClassLoader ,继承 URLClassLoader package com.tomcat7.test; import java.net.URL; import java.net.URLClassLoader; public class MyClassLoader extends URLClassLoader{ public MyClassLoader(URL[] urls) { super(urls); } public Class<?> findClass(final String name) throws ClassNotFoundException { return super.findClass(name); } } 这里说下为什么要继承,而不直接使用 URLClassLoader ,是因为 findClass 是 protected 的,详情可查看 URLClassLoader 源码 2.编写测试类,里面没有直接引入其它package package com.tomcat7.test; import java.io.File; import java.lang.reflect.Method; import java.net.URL; public class ClassLoader { public

[tomcat7源码学习]ClassLoader加载Tomcat的依赖

白昼怎懂夜的黑 提交于 2019-12-01 16:32:25
ClassLoader加载Tomcat的依赖 通过查看catalina.bat中设置的 classpath 只有 bootstrap.jar 和 tomcat-juli.jar ,这两个都只包含 Bootstrap 执行时所需要的类,所以如果要进行后续操作,都需要再加载。通过前面返回的ClassLoader加载 1.在 BootStrap 中加载完成 ClassLoader 之后就回到了 init 方法中. Thread.currentThread().setContextClassLoader(catalinaLoader); SecurityClassLoad.securityClassLoad(catalinaLoader); 这里就开始加载tomcat所需要的class 2.进入 SecurityClassLoad.securityClassLoad public static void securityClassLoad(ClassLoader loader) throws Exception { if( System.getSecurityManager() == null ){ return; } loadCorePackage(loader); loadCoyotePackage(loader); loadLoaderPackage(loader);