在特定文件夹中仅使用 类 创建类加载器
Create Classloader with only classes inside specific folder
我想使用 specific ClassLoader
在 ScriptEngineManager
中加载特定的 jar
This constructor loads the implementations of ScriptEngineFactory visible to the given ClassLoader using the service provider mechanism.
我尝试创建时的问题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 URLClassLoader(urls)
我也得到 class 不在 class 例如 Spring
Class cls1 = cl.loadClass("org.springframework.http.HttpStatus");
如何创建一个干净的 classloader,只包含特定文件夹的 classes?
编辑
如果不可能,我可以在 groovy 脚本中使用与 Rhino 的 ClassShutter
等效的东西吗
private static void setJavaClassesVisibleInvisibleSandbox(Context cx)
{
cx.setClassShutter(new ClassShutter()
{
public boolean visibleToScripts(String className)
{
// No Java classes allowed inside scripts
编辑
当尝试@Vinz243 CustomClassLoader
解决方案无法加载 classes 的 classes 文件或来自特定文件夹的 jar 文件,甚至尝试 rt.jar
java.lang.SecurityException: Prohibited package name: java.lang
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:662)
at java.lang.ClassLoader.defineClass(ClassLoader.java:761)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access0(URLClassLoader.java:73)
at java.net.URLClassLoader.run(URLClassLoader.java:368)
at java.net.URLClassLoader.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at com.CustomClassLoader.loadClass(CustomClassLoader.java:15)
如 javadoc 中所述:
public URLClassLoader(URL[] urls)
Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader. The URLs will be searched in the order specified for classes and resources after first searching in the parent class loader. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.
因此您可以尝试扩展 ClassLoader
,因为负责加载父 class 的机制在 java.lang.ClassLoader#loadClass(java.lang.String, boolean)
中
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
编辑
未测试,但类似的东西应该可以完成工作:
class CustomClassLoader extends URLClassLoader {
public CustomClassLoader (URL[] urls) throws NoSuchMethodException {
super(urls);
}
@Override
protected Class<?> loadClass (String name, boolean resolve) throws ClassNotFoundException {
synchronized (getClassLoadingLock(name)) {
Class<?> aClass = findClass(name);
if (resolve) {
resolveClass(aClass);
}
return aClass;
}
}
}
我想使用 specific ClassLoader
在ScriptEngineManager
中加载特定的 jar
This constructor loads the implementations of ScriptEngineFactory visible to the given ClassLoader using the service provider mechanism.
我尝试创建时的问题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 URLClassLoader(urls)
我也得到 class 不在 class 例如 Spring
Class cls1 = cl.loadClass("org.springframework.http.HttpStatus");
如何创建一个干净的 classloader,只包含特定文件夹的 classes?
编辑
如果不可能,我可以在 groovy 脚本中使用与 Rhino 的 ClassShutter
等效的东西吗private static void setJavaClassesVisibleInvisibleSandbox(Context cx) { cx.setClassShutter(new ClassShutter() { public boolean visibleToScripts(String className) { // No Java classes allowed inside scripts
编辑
当尝试@Vinz243 CustomClassLoader
解决方案无法加载 classes 的 classes 文件或来自特定文件夹的 jar 文件,甚至尝试 rt.jar
java.lang.SecurityException: Prohibited package name: java.lang
at java.lang.ClassLoader.preDefineClass(ClassLoader.java:662)
at java.lang.ClassLoader.defineClass(ClassLoader.java:761)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access0(URLClassLoader.java:73)
at java.net.URLClassLoader.run(URLClassLoader.java:368)
at java.net.URLClassLoader.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at com.CustomClassLoader.loadClass(CustomClassLoader.java:15)
如 javadoc 中所述:
public URLClassLoader(URL[] urls)
Constructs a new URLClassLoader for the specified URLs using the default delegation parent ClassLoader. The URLs will be searched in the order specified for classes and resources after first searching in the parent class loader. Any URL that ends with a '/' is assumed to refer to a directory. Otherwise, the URL is assumed to refer to a JAR file which will be downloaded and opened as needed.
因此您可以尝试扩展 ClassLoader
,因为负责加载父 class 的机制在 java.lang.ClassLoader#loadClass(java.lang.String, boolean)
try {
if (parent != null) {
c = parent.loadClass(name, false);
} else {
c = findBootstrapClassOrNull(name);
}
} catch (ClassNotFoundException e) {
// ClassNotFoundException thrown if class not found
// from the non-null parent class loader
}
编辑
未测试,但类似的东西应该可以完成工作:
class CustomClassLoader extends URLClassLoader {
public CustomClassLoader (URL[] urls) throws NoSuchMethodException {
super(urls);
}
@Override
protected Class<?> loadClass (String name, boolean resolve) throws ClassNotFoundException {
synchronized (getClassLoadingLock(name)) {
Class<?> aClass = findClass(name);
if (resolve) {
resolveClass(aClass);
}
return aClass;
}
}
}