classloader

How to replace classes in a running application in java ?

本秂侑毒 提交于 2019-11-27 18:49:26
Say I have a class named NameGenerator . I can use this to generate names according to a given logic. Then I write a TestNameGeneration class with a method that asks for a letter from the user and generate a name in accordance. Now I want to change the logic in NameGeneration class and apply that particular change without stopping the application. I did this to learn more about class loaders and can someone please explain the key concepts that I have to learn to do something like that or site any references ? Here is a working test. Every 5 secs Test.main() reloads test.Test1.class from the

Is the defining classloader of a class level annotation always a parent of the initiating classloader of that class?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 17:53:08
问题 Suppose the following: @SomeAnnotation public interface Foo { } I would like to know if it is always the case that either the defining classloader of SomeAnnotation is equal to or a parent of the initiating classloader of Foo . I have read JVMS v8 section 5.3. but I'm not sure what applies here. Section 5.3.4 talks about loading constraints, but they seem not to apply for annotations. The question I'm asking is because code like this: Class<?> fooClass = //will in some way obtain a reference

How to explore which classes are loaded from which JARs?

自古美人都是妖i 提交于 2019-11-27 17:51:37
Is there a way to determine which classes are loaded from which jars at runtime? I'm sure we've all been in JAR hell before. I've run across this problem a lot troubleshooting ClassNotFoundException s and NoClassDefFoundError s on projects. I'd like to avoid finding all instances of a class in jars and using process of elimination on the code causing a CNFE to find the culprit. Will any profiling or management tools give you this kind of information? This problem is super annoying purely because we should have this information at the time the class gets loaded. There has to be a way to get to

Load a Byte Array into a Memory Class Loader

假如想象 提交于 2019-11-27 17:20:37
问题 I am wondering how I can load a byte array into a memory URLClassLoader? The byte array is the decrypted bytes of a jar file (as seen below)! Most of the memory class loaders are using ClassLoader and not URLClassLoader! I need it to be using URLClassLoader. byte[] fileB = Util.crypt.getFileBytes(inputFile); byte[] dec; dec = Util.crypt.decrypt(fileB, "16LENGTHLONGKEYX".getBytes()); //Load bytes into memory and load a class here? Thanks! 回答1: I will post here a implementation I had done in

How can I implement an abstract singleton class in Java?

怎甘沉沦 提交于 2019-11-27 17:16:42
问题 Here is my sample abstract singleton class: public abstract class A { protected static A instance; public static A getInstance() { return instance; } //...rest of my abstract methods... } And here is the concrete implementation: public class B extends A { private B() { } static { instance = new B(); } //...implementations of my abstract methods... } Unfortunately I can't get the static code in class B to execute, so the instance variable never gets set. I have tried this: Class c = B.class; A

How to use two versions of jar in my java project

孤街醉人 提交于 2019-11-27 16:36:40
问题 In my java project, I need to use neo4j-1.9.3 that depends on lucene-3.6.2 , and ElasticSearch which depends on lucene-4.4.0 . I know that if I want to use two versions of lucene directly, I can use ClassLoader to load different classes from the lucenes. But the problem is that I won't use lucene's apis directly now. Is there any way that lucene-3.6.2 can be loaded when neo4j's apis are running, and lucene-4.4.0 can be loaded while running elasticsearch's apis. The two versions of lucene

Rails doesn't load classes on deserializing YAML/Marshal objects

本小妞迷上赌 提交于 2019-11-27 16:17:49
问题 Rails: 3.0.3 Ruby: 1.9.2 Trying to deserialize a very simple object using YAML.load or Marshal.load produces a corrupted object because the class which belongs to is not required on the deserializing process. Example: # app/models/my_model.rb class MyModel attr_accessor :id end # test/unit/serializing_test.rb require 'test_helper' class SerializingTest < Test::Unit::TestCase def test_yaml_serialize_structure my_model = MyModel.new my_model.id = 'my model' File.open( "#{Rails.root}/tmp/object

Change classloader

喜夏-厌秋 提交于 2019-11-27 16:16:12
问题 I'm trying to switch the class loader at runtime: public class Test { public static void main(String[] args) throws Exception { final InjectingClassLoader classLoader = new InjectingClassLoader(); Thread.currentThread().setContextClassLoader(classLoader); Thread thread = new Thread("test") { public void run() { System.out.println("running..."); // approach 1 ClassLoader cl = TestProxy.class.getClassLoader(); try { Class c = classLoader.loadClass("classloader.TestProxy"); Object o = c

Java 8 ScriptEngine across ClassLoaders

我们两清 提交于 2019-11-27 15:18:19
I need to execute some javascript code 'inside' different classloaders. If it is java, each task will run in separate class loader. Now I need this to be javascript. Do I need to create new instance of ScriptEngine in each classloader, or is it ok to share one across class loaders? From your question it is not clear why'd you look for such classloader isolation. So, I'm summarizing nashorn's classloader here - may be, you'll get what you're looking for. Nashorn and classloaders: Nashorn classes (jdk.nashorn.*) are loaded by Java extension class loader Generated script classes, adapters

How to provide an interface to JavaCompiler when compiling a source file dynamically?

╄→гoц情女王★ 提交于 2019-11-27 15:07:42
问题 I'm trying to compile and load a class at runtime, without knowing the package of the class. I do know that the class should comply with an interface, and the location of the source, (and hence the class name). I'm trying the following: /* Compiling source */ File root = new File("scripts"); File sourceFile = new File(root, "Test.java"); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); compiler.run(null, null, null, sourceFile.getPath()); where the Test.java file looks something