urlclassloader

ServiceLoader using ClassLoader pointing to different path

人走茶凉 提交于 2019-12-11 08:12:04
问题 Have been trying this since few days and can't get it working! I am trying to build a pluggable java application where I can run it from command line and provide plugins (jars) in a separated folder. It seems the ServiceLoader would fit my requirement but I think I need a special case where the jars are not part of the classpath whereas they are stored in a different location, and for this reason I would need to use a ClassLoder pointing its url to this file system path. One of the plugin i

NoClassDefFoundError when adding classes to the classpath at runtime when I used abstract or interface in Main class

孤人 提交于 2019-12-11 04:44:49
问题 I know Java loads Classes in first Access (creating new Instance, calling static method or static field), but in this simple example I try to execute a jar file that uses some classes which there aren't in my ClassPath at run time. I expect (because of loading classes in first access) print my messages in static block and main method before an exception occurred. but I got "Exception in thread "main" java.lang.NoClassDefFoundError: com/example/DateAbstract" and nothing printed. This occurred

Though my class was loaded, Class.forName throws ClassNotFoundException

柔情痞子 提交于 2019-12-11 02:54:45
问题 The code is as follows what it does is it loads all the classes inside a jar file which I placed inside my home directory . import java.io.File; import java.util.jar.JarFile; import java.util.jar.JarEntry; import java.net.URLClassLoader; import java.net.URL; import java.util.Enumeration; import java.lang.ClassLoader; public class Plugin extends ClassLoader { public static void main(String[] args) throws Exception { File file = new File(System.getProperty("user.home") + "/HelloWorld.jar");

Does URLClassLoader.addURL(URL) honor the META-INF/MANIFEST.MF

一曲冷凌霜 提交于 2019-12-10 10:46:04
问题 If I have a jar that includes a Class-Path entry in the MANIFEST.MF . If I add the jar to a URLClassLoader , are the jars in the Class-Path entry also added to the classloader ? Do I need to introspect the jars that I want to add to the classloader to detect this and call addURL for each of them (recursively)? 回答1: It does as of JDK 1.6 according to the source code of sun.misc.URLClassPath, but it isn't specified, so take your pick ;-) 来源: https://stackoverflow.com/questions/10908573/does

Listing classes in a jar file

与世无争的帅哥 提交于 2019-12-09 10:06:52
问题 How can I dynamically load a jar file and list classes which is in it? 回答1: Have a look at the classes in the package java.util.jar . You can find examples of how to list the files inside the JAR on the web, here's an example. (Also note the links at the bottom of that page, there are many more examples that show you how to work with JAR files). 回答2: Here is code for listing classes in jar: import java.io.IOException; import java.util.Enumeration; import java.util.jar.JarEntry; import java

Reload class file in tomcat

好久不见. 提交于 2019-12-08 06:11:11
问题 I am creating a class file at run time. I want to replace the existing class file with the updated one in the class loader. It is similar to hot swapping (e.g. JRebel) which avoids server restart and redeployment. I found context.xml approach for tomcat for context reloading. But it is not very useful in case of production environment. Can we register class with ClassLoader at runtime? Please suggest if any alternative is there to reload class at runtime. I am using following code to retrieve

How to load all the jars from a directory dynamically?

こ雲淡風輕ζ 提交于 2019-12-08 03:18:50
问题 Hi I am creating a plugin which requires loading jars dynamically and accessing the classes and methods of these jars. I tried using URLClassLoader and am able to load the classes as shown below URL myJarFile = new URL("jar","","file:"+jarPath); URLClassLoader sysLoader =(URLClassLoader)ClassLoader.getSystemClassLoader(); Class sysClass = URLClassLoader.class; Method sysMethod = sysClass.getDeclaredMethod("addURL", new Class[]{URL.class}); sysMethod.setAccessible(true); sysMethod.invoke

Detect Java JAR/Code Tampering

亡梦爱人 提交于 2019-12-08 03:05:44
问题 I'm writing a piece of software that is distributed as a JAR file. Currently, this JAR file can be tampered with to retrieve and save another file that our server transmits via URLClassLoader , be decompiled, and find various things in our code that should remain private for the security of the clients using it. Basically, I want to implement a way to check if the original JAR is tampered with. I know this is paradoxically impossible by implementing a check for validity of a SignedObject in

Create Classloader with only classes inside specific folder

时光怂恿深爱的人放手 提交于 2019-12-07 15:53:51
问题 I want to load specific jars in ScriptEngineManager using specific ClassLoader This constructor loads the implementations of ScriptEngineFactory visible to the given ClassLoader using the service provider mechanism. The problem when I tried to create Classloader File file = new File("c:\\myclasses\\"); try { // Convert File to a URL URL url = file.toURI().toURL(); // file:/c:/myclasses/ URL[] urls = new URL[]{url}; // Create a new class loader with the directory ClassLoader cl = new

Java 9 - add jar dynamically at runtime

被刻印的时光 ゝ 提交于 2019-12-07 04:56:20
问题 I've got a classloader problem with Java 9. This code worked with previous Java versions: private static void addNewURL(URL u) throws IOException { final Class[] newParameters = new Class[]{URL.class}; URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); Class newClass = URLClassLoader.class; try { Method method = newClass.getDeclaredMethod("addNewURL", newParameters ); method.setAccessible(true); method.invoke(urlClassLoader, new Object[]{u}); } catch