classloader

Java classLoader dilemma with locked jars

江枫思渺然 提交于 2019-12-01 05:18:39
I was playing around with classLoaders in Java and noticed a strange thing. If a classLoader loads a class from a jar, this jar is locked indefinitely even if you unreference your classLoader. In the below example, the jar contains a class called HelloWorld. What I do is try to load the class contained in the jar via a classLoader which adds the jar dynamically. If you set skip to true and do not call Class.forName , you can delete the jar but if you do not skip and even if you unreference the classLoader ( classLoader = null ), the jar cannot be deleted until the JVM exits. Why is that? PS: I

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

馋奶兔 提交于 2019-12-01 05:12:31
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 than main (assuming that it is main on the doRun object that is called with the run invocation)? Just

Use a custom classloader at compile time

て烟熏妆下的殇ゞ 提交于 2019-12-01 05:08:52
问题 Is it possible to specify a custom classloader for javac (or some alternative java compiler)? I'd love such a feat because it would allow me to compile classes that use classes that are only found by my special classloader. For the curious ones: I'd write a classloder that connects to a database and creates classes based on the tables it finds. 回答1: When you run javac you can specify the classloader like so: javac -J-Djava.system.class.loader=org.awesome.classloader sourcefile.java 回答2: It

Custom Tomcat Valve contained in web app WAR file

我的未来我决定 提交于 2019-12-01 04:37:26
I'm looking to implement a custom Valve and configuring it using META-INF/context.xml. At the moment though, when context.xml is parsed during deployment, Tomcat (6.0.32) dies with a ClassNotFoundException on my custom Valve implementation. I'm under the impression that I'm running into a class loading context issue and I'm not 100% sure I understand it. Is my class not found because it is located in the WEB-INF/classes file and the Context level class loader is unable to locate the class because of the hierarchy? Thanks in advance. You can not load Valve s from inside the webapp class loader.

Replace implementation of instantiated class without touching the code (java)

不想你离开。 提交于 2019-12-01 04:37:07
问题 I have legacy code I don't want to touch. public class LegacyCode{ public LegacyCode() { Service s = new ClassA(); s.getMessage(); } } Where ClassA provides a CORBA service call. public class ClassA implements Service{ @Override public String getMessage() { // Make CORBA service call... return "Class A"; } } And the interface Service looks like; public interface Service { String getMessage(); } For test purposes, I want to replace the implementation of Service (in LegacyCode implemented by

Native Library already loaded in another classloader [duplicate]

隐身守侯 提交于 2019-12-01 04:35:07
This question already has an answer here: .dll already loaded in another classloader? 4 answers I need some help to handle following scenario. I am using two applets which requires the same native library (.dll) file. So when I run the applets from the web pages, for the first time first applet loads the dll into the applet class loader. It works fine. But when second applet tries to load the same dll it gives me exception saying that "Error loading win32com: java.lang.UnsatisfiedLinkError: Native Library C:\WINDOWS\system32\win32com.dll already loaded in another classloader" I using following

In Java, can Class.forName ever return null?

放肆的年华 提交于 2019-12-01 04:18:40
问题 In Java, can Class.forName ever return null, or will it always throw a ClassNotFoundException or NoClassDefFoundError if the class can't be located? 回答1: Java Docs says it will throw ClassNotFoundException if the class cannot be found so I'd say it never returns null. 回答2: Since null is not mentioned anywhere in the documentation for this method and because there doesn't seem to be any situation in which it would make sense for the method to return null instead of throwing an exception, I

How JVM loads parent classes in Java

强颜欢笑 提交于 2019-12-01 03:31:27
问题 Code: class A { static { System.out.println("loading A static 1"); } static { System.out.println("loading A static 2 B.c= "+B.c); } static { System.out.println("loading static 3"); } static int a=10; A(){ } } class B extends A{ static { System.out.println("loading B A.a= "+A.a); } static int c = 50; } public class Test { public static void main(String[] args) { new B(); } } Output: loading A static 1 loading A static 2 B.c= 0 loading static 3 loading B A.a= 10 From this out put can we say

Native Library already loaded in another classloader [duplicate]

给你一囗甜甜゛ 提交于 2019-12-01 03:28:24
问题 This question already has answers here : .dll already loaded in another classloader? (4 answers) Closed 6 years ago . I need some help to handle following scenario. I am using two applets which requires the same native library (.dll) file. So when I run the applets from the web pages, for the first time first applet loads the dll into the applet class loader. It works fine. But when second applet tries to load the same dll it gives me exception saying that "Error loading win32com: java.lang

eclipselink PersistenceUnitLoadingEception in executable JAR

烂漫一生 提交于 2019-12-01 03:13:14
问题 I am developing a stand-alone java application which uses eclipselink. It is all fine when I execute the app from eclipse IDE. But I've exported an executable JAR file, and since than I can not make eclipseLink work. I have found similar issue in the Eclipse community forum here, but yet not too handy: Please help, My exception is the following: 01 dec. 2010 22:47:31,199 INFO Configuration:97 - Iniciate database Exception in thread "main" java.lang.reflect.InvocationTargetException at sun