classloader

Loading Encrypted JarFile Via URLCassloader

风格不统一 提交于 2019-12-03 22:28:22
I've been writing a little system to dynamically load AES encrypted jar files. My code: public static void main(String args[]) throws Exception { String jar = "http://site.com/api/rsc/test.jar"; List<URL> urls = new ArrayList<URL>(); urls.add(getURL(jar)); URL jarurl = urls.get(0); ObjectInputStream ois = new ObjectInputStream((new URL("http://site.com/api/rsc/key_1.txt").openStream())); Object o = ois.readObject(); DESKeySpec ks = new DESKeySpec((byte[])o); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey key = skf.generateSecret(ks); Cipher c = Cipher.getInstance("DES

URLClassLoader loads Annotation as com.sun.$Proxy$27

て烟熏妆下的殇ゞ 提交于 2019-12-03 21:14:08
问题 I'm trying to dynamically load a java class. The basic idea is, that a jar contains modules which get loaded dynamically at runtime. This is how I do it (I know it's hacky, but there is no other method to dynamically add a jar to an already existing classloader afaik): Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] { URL.class }); method.setAccessible(true); method.invoke(moduleLoader, new Object[] { file.toURI().toURL() }); Class fooClass = moduleLoader

Where does bytecode injection happen?

家住魔仙堡 提交于 2019-12-03 21:12:30
Motivation I have a SomeObject.java file: class SomeObject { String name; } Compiling it creates a bytecode-containing SomeObject.class file. 0xCAFEBABE... If we use SomeObject on the JVM, it is loaded by the current classloader and all works fine. Now let's assume that I'd like to have some dynamic code generation. I can write my custom annotation @Target(ElementType.TYPE) public @interface Data { ... } and add it as a modifier to the class declaration: @Data class SomeObject { String name; } I can also retain it for the runtime with @Retention(RetentionPolicy.RUNTIME) : @Retention

How do I get a list of packages and/or classes on the classpath?

我们两清 提交于 2019-12-03 20:57:44
In Java, I can use a ClassLoader to get a list of classes that are already loaded, and the packages of those classes. But how do I get a list of classes that could be loaded, i.e. are on the classpath? Same with packages. This is for a compiler; when parsing foo.bar.Baz, I want to know whether foo is a package to distinguish it from anything else. Its a bit tricky and there are a few libraries that can help, but basically... Look at your classpath If you are dealing with a directory, you can look for all files ending in .class If you are dealing with a jar, load the jar up and look for all

Resolving the root of a webapp from getResource

☆樱花仙子☆ 提交于 2019-12-03 20:36:51
I have a file structure like this in my webapp: webapp/ ├── META-INF └── WEB-INF ├── reports │ └── info.txt └── web.xml 3 directories, 2 files I need to get /WEB-INF/reports/info.txt from a class like so: this.getClass().getResource("/WEB-INF/reports/info.txt"); Will this resolve? I'm going to test it, but I'm not sure how Tomcat's classloader resolves things. If this doesn't work, how can I get the file? Well, first of all, this.getClass().getResource should not work (though I didn't try). It's not a classpath, it's a ServletContext, so you need to use ServletContext.getResource . Problem is,

How does Grails handle plugin dependencies

↘锁芯ラ 提交于 2019-12-03 20:19:37
问题 I'm creating a Grails Plugin as a wrapper for a complex product. This product has a lot of dependencies to other products like hibernate. The issue is, that grails has some same dependencies but with different Versions. E.g. Grails -> hibernate 3.6.7 other product -> hibernate 3.5.6 How does Grails handle the plugin dependencies? Does Grails create an separate ClassLoader for each Plugin? Is it configurable? Thanks in advance! 回答1: Grails has a dependency resolution mechanism that resolves

Loading a class twice in JVM using different loaders

☆樱花仙子☆ 提交于 2019-12-03 20:15:53
问题 I have one question regarding concepts of class loading. How to load a .class file twice in JVM. I am also writing an excerpt of code that I have written to accomplish this.. 1) Loader 1 code public class MyClassLoader extends ClassLoader { public MyClassLoader(){ super(MyClassLoader.class.getClassLoader()); } public Class loadClass(String classname){ try { return super.loadClass(classname); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return

Class.forName("xxx")调用过程

大兔子大兔子 提交于 2019-12-03 18:50:39
问题: java.lang.ClassNotFoundException 1.Class.forName("xxx")作用概述 xxx:字符串,表示要装载的类名或者是驱动名字。 Class.forName()的主要作用是将“xxx”表示的类装载到JAVA虚拟机中。 2.Class.forName("xxx")具体加载过程 解决的问题: “xxx”表示类的字节码应该放在哪里? Class.forName("xxx")调用过程的相关的类、对象和系统环境? 设计Class.forName("xxx")的意义? 2.1 Class.forName("xxx")寻找过程细节? 这里主要解决前两个问题—— “xxx”表示类的字节码应该放在哪里?和Class.forName("xxx")调用过程的相关的类、对象和系统环境? java.sql.DriverManager.registerDriver(new Driver());【1】 在这篇博文中,表示java.sql.DriverManager是在Class.forName("xxx")中的xxx类放入内存后,才静态注册驱动类。但是JVM如何将xxx放入内存呢?下面是Class类中forName()的源代码 public static Class<?> forName(String className) throws

Java 9.0 | ClassLoader::getResourceAsStream: NullPointerException

老子叫甜甜 提交于 2019-12-03 17:31:39
问题 This piece of code down below, where I take my file from folder which is inside the " /resource " folder, works fine for me in Java 8: //e.g fileName = "folder0/file1.extension2" ClassLoader classLoader = ResourceLoader.class.getClassLoader(); InputStream in = classLoader.getResourceAsStream(fileName); Scanner scanner = new Scanner(in, "UTF-8"); In Java 9 it does not, classLoader.getResourceAsStream(fileName) returns null: java.lang.NullPointerException: source However, if I use files

How do I access the content of folders inside a jar file?

元气小坏坏 提交于 2019-12-03 17:11:52
I need to take a look at the names of files inside a specific package. Currently, I'm doing the following: ClassLoader loader = Thread.currentThread().getContextClassLoader(); URL packageUrl = loader.getResource(getPackage().replace('.', '/')); File packageDir = new File(packageUrl.getFile()); // Work with the files inside the directory This actually works just fine in Eclipse, since it doesn't package my application by default. When I run it from a jar file, however, I get a NullPointerException on packageUrl . So, how do I obtain a list of the contents of a package that's inside a jar? My