dynamic-class-loaders

get CtClass using specific ClassLoader

ぐ巨炮叔叔 提交于 2021-02-10 18:44:44
问题 I have a class structure like this: package com.mydomain.myproject; public class Outer{ public class Inner{ //some code } } Now, I can get a CtClass of the inner class using: ClassPool pool=ClassPool.getDefault(); CtClass innerCt=pool.getCtClass("com.mydomain.myproject.Outer$Inner"); The problem occurs if those classes are loaded by a special ClassLoader . ClassPool#getCtClass fails because the ClassLoader it uses doesn't know of the class. I get the following exception: javassist

get CtClass using specific ClassLoader

纵饮孤独 提交于 2021-02-10 18:41:10
问题 I have a class structure like this: package com.mydomain.myproject; public class Outer{ public class Inner{ //some code } } Now, I can get a CtClass of the inner class using: ClassPool pool=ClassPool.getDefault(); CtClass innerCt=pool.getCtClass("com.mydomain.myproject.Outer$Inner"); The problem occurs if those classes are loaded by a special ClassLoader . ClassPool#getCtClass fails because the ClassLoader it uses doesn't know of the class. I get the following exception: javassist

Using URLClassLoader to load .class file

我只是一个虾纸丫 提交于 2021-02-05 05:55:05
问题 I'm aware that this question has been asked before: How to use URLClassLoader to load a *.class file? However I don't really understand due to the lack of example. I'm currently working on a project and trying to load user-given .class objects which can be located in any directories on the machine. //Create URL to hash function class file URL url_hashFunctionPath = new URL("file:///" + _sHashFunctionFilePath); //Packet URL to a URL array to be used by URLClassLoader URL[] urlA

Custom ClassLoader, how to use?

為{幸葍}努か 提交于 2020-05-27 05:36:09
问题 I am trying to use a custom class loader to load all the dependencies needed for the application. I've implemented the customerClassLoader following the site: https://www.javacodegeeks.com/2013/03/java-handmade-classloader-isolation.html However, I dont understand how to tell my application to use the custom classLoader whenever needed. For instance: Lets say, I have a method to make http request like below. How can I tell the application to use the custom classLoader to load the required

Java: Where does the Extensions Class Loader get classes from in Java 13?

早过忘川 提交于 2020-05-23 09:54:24
问题 All the documentation I've been able to find mentions the 'jre/lib/ext' folder but such does not exist on my JRE 13 installation. I guess somewhere between Java 8 (where I can see the jars in jre/lib/ext) and Java 13, they moved but I've been unable to pinpoint when and how it was done. Could someone please elaborate what's going on with new JRE's, in terms of where the extension classes reside currently? 回答1: The extension mechanism is gone with Java 9, not only moved [:-| , see the

Java: Where does the Extensions Class Loader get classes from in Java 13?

非 Y 不嫁゛ 提交于 2020-05-23 09:54:17
问题 All the documentation I've been able to find mentions the 'jre/lib/ext' folder but such does not exist on my JRE 13 installation. I guess somewhere between Java 8 (where I can see the jars in jre/lib/ext) and Java 13, they moved but I've been unable to pinpoint when and how it was done. Could someone please elaborate what's going on with new JRE's, in terms of where the extension classes reside currently? 回答1: The extension mechanism is gone with Java 9, not only moved [:-| , see the

java.lang.StringIndexOutOfBoundsException: String index out of range when package name is changed

牧云@^-^@ 提交于 2020-04-30 06:30:37
问题 I want to read the annotations from .class file placed into random folder. I tried this: public static void main(String[] args) throws Exception { final File folder = new File("/opt/test"); processAnnotatedFiles(listLocalFilesAndDirsAllLevels(folder)); } public void processAnnotatedFiles(List<File> list) throws IOException, ClassNotFoundException { out.println("Directory files size " + list.size()); for(int i=0; i<list.size(); i++) { out.println("File " + list.get(i).getName()); File file =

Class.forName(name, instantiation, classLoader) doesn't add class to classpath

那年仲夏 提交于 2020-04-12 06:02:07
问题 I'm generating .java class files at the runtime and need to utilize those classes inside the code instantly. So I compile the .java classes using Compiler API to make .class files: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); StandardJavaFileManager manager = compiler.getStandardFileManager(diagnostics, null, null); File file = new File("path to file"); Iterable<? extends JavaFileObject> sources =

Class.forName(name, instantiation, classLoader) doesn't add class to classpath

房东的猫 提交于 2020-04-12 06:01:46
问题 I'm generating .java class files at the runtime and need to utilize those classes inside the code instantly. So I compile the .java classes using Compiler API to make .class files: JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>(); StandardJavaFileManager manager = compiler.getStandardFileManager(diagnostics, null, null); File file = new File("path to file"); Iterable<? extends JavaFileObject> sources =

Dynamic loading a class in java with a different package name

邮差的信 提交于 2020-01-10 10:28:49
问题 Is it possible to load a class in Java and 'fake' the package name/canonical name of a class? I tried doing this, the obvious way, but I get a "class name doesn't match" message in a ClassDefNotFoundException . The reason I'm doing this is I'm trying to load an API that was written in the default package so that I can use it directly without using reflection. The code will compile against the class in a folder structure representing the package and a package name import. ie: ./com