classloader

MyClass.class.getClassLoader().getResource(“”).getPath() throws NullPointerException

谁都会走 提交于 2019-12-22 18:00:40
问题 I have code that is running correctly on a development machine but throws a NullPointerException when installed in a production environment. The line that throws the exception is the following: MyClass.class.getClassLoader().getResource("").getPath(); So I split this into multiple lines of code to see exactly which call was returning null, like this: ClassLoader cl = MyClass.class.getClassLoader(); URL url = cl.getResource(""); String path = url.getPath(); Now the url.getPath() call is

图解classloader加载class的流程及自定义ClassLoader

爷,独闯天下 提交于 2019-12-22 17:41:50
【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>> java应用环境中不同的class分别由不同的ClassLoader负责加载。 一个jvm中默认的classloader有Bootstrap ClassLoader、Extension ClassLoader、App ClassLoader,分别各司其职: Bootstrap ClassLoader 负责加载java基础类,主要是 %JRE_HOME/lib/ 目录下的rt.jar、resources.jar、charsets.jar和class等 Extension ClassLoader 负责加载java扩展类,主要是 %JRE_HOME/lib/ext 目录下的jar和class App ClassLoader 负责加载当前java应用的classpath中的所有类。 其中Bootstrap ClassLoader是JVM级别的,由C++撰写;Extension ClassLoader、App ClassLoader都是java类,都继承自URLClassLoader超类。 Bootstrap ClassLoader由JVM启动,然后初始化sun.misc.Launcher ,sun.misc.Launcher初始化Extension ClassLoader、App ClassLoader。

Is it possible to “explore” which objects are defined within an other object via reflection at runtime?

无人久伴 提交于 2019-12-22 12:51:07
问题 Consider this code: object A { object AA extends A object AB extends A object AC extends A }; class A How is it possible to "see" the objects defined within object A at runtime? I thought a method within object A with some simple reflection code would be enough, but it seems that the compiler flattens the object hierarchy at compile time and created the following class files: A.class – The class A A$class – The companion object of A A$AA$.class – The AA object A$AB$.class – The AB object A$AC

Class Loading in JVM

戏子无情 提交于 2019-12-22 11:35:54
问题 When does the class get loaded in the JVM? Do they get loaded on the server start up or when there is a reference for the class? My assumption is that all the class gets loaded as the server like jboss starts up but then there is something called lazyloading. Also what is actually meant by loading? Does it mean there is this .class in the JVM memory along with all the methods, variables including instance, static variables methods and are available for execution. I Know that ClassLoader

Dynamic class reloading works only in debug mode, why/ how it really works?

你说的曾经没有我的故事 提交于 2019-12-22 11:04:15
问题 I have a tricky question. My java program is doing in a loop such things: loop: read external file with compiled java class into byte[] array. create a new instance of my own classloader. setbytes from the readed file to this instance of classloader. using created classloader create a new instance of object of the class from external file. call any method of the created object. And where the problem is. When I run this program in debug mode it behaves as I expect, so if external file changed

Tomcat classloader violates delegating policy

谁说胖子不能爱 提交于 2019-12-22 08:15:03
问题 Question1: As we know, when a classloader is about to load a class, it delegates the request to its parent classloader. However in Tomcat, it doesn’t: you could load your class to overwrite the same name class which is put in common lib directory. This means Tomcat WebappClassloader doesn’t follow delegating policy. Is it violation of convention? Question2: I wrote a class and put it in common lib directory, obviously the class is shared among web apps. For instance, every web app can read

when use junit4 + powermock to execute all test suites , I got an error : swt-win32-3650.dll already loaded in another classloader

强颜欢笑 提交于 2019-12-22 08:12:04
问题 when use junit4 + powermock to execute all test suites , I got an error : swt-win32-3650.dll already loaded in another classloader alltest.java: @RunWith( Suite.class ) @SuiteClasses( {test1.class, test2.class} ) public class AllTests { } test1.java @RunWith( PowerMockRunner.class ) @PrepareOnlyThisForTest( {Object.class} ) public class test1 extends TestCase { @Test public void testcase() { Shell sh = Mockito.mock( Shell.class ); PowerMockito.when( sh.getText() ) .thenReturn( this.getClass()

How to load a resource from an embedded JAR File

余生长醉 提交于 2019-12-22 07:43:23
问题 I am trying to load a resource that is contained within an embedded JAR file. The project is actually deployed in JBoss using an EAR file with the following structure: deploy.ear | |-> project.sar | |-> sub_project.jar | | | |-> settings.xml | |-> com/path/project/ | |-> main.class From main.java I'd like to get a InputStream for settings.xml . What is the correct way to do this? My current understanding that the following code should work, but it is returning null : this.getClass()

How to load a resource from an embedded JAR File

拟墨画扇 提交于 2019-12-22 07:43:07
问题 I am trying to load a resource that is contained within an embedded JAR file. The project is actually deployed in JBoss using an EAR file with the following structure: deploy.ear | |-> project.sar | |-> sub_project.jar | | | |-> settings.xml | |-> com/path/project/ | |-> main.class From main.java I'd like to get a InputStream for settings.xml . What is the correct way to do this? My current understanding that the following code should work, but it is returning null : this.getClass()

Reading file from within JAR not working on windows

雨燕双飞 提交于 2019-12-22 07:39:36
问题 I'm trying to read from a file that is packaged up inside a JAR, along with the class that reads it. To do this, I use the following: getClass().getClassLoader().getResourceAsStream(file) This works fine when I create and run the JAR file on OSX, but if I create and run the JAR file on windows, the above line returns null. Am I missing something here? If I create the JAR on OSX and run it on Windows it works fine. The problem only occurs when I create the JAR on windows. EDIT: It's worth