런타임에 Java 클래스 경로에 파일 추가
이 질문에 이미 답변이 있습니다.
런타임에 Java 클래스 경로에 파일 (jar 파일 일 필요는 없음)을 추가 할 수 있습니까? 특히 파일이 이미 클래스 경로에 있습니다. 내가 원하는 것은이 파일의 수정 된 사본을 클래스 경로에 추가 할 수 있는지 여부입니다.
감사,
클래스 로더에는 폴더 또는 jar 파일 만 추가 할 수 있습니다. 따라서 단일 클래스 파일이있는 경우 먼저 해당 파일을 적절한 폴더 구조에 넣어야합니다.
다음 은 런타임에 SystemClassLoader에 추가하는 다소 추한 해킹입니다.
import java.io.IOException;
import java.io.File;
import java.net.URLClassLoader;
import java.net.URL;
import java.lang.reflect.Method;
public class ClassPathHacker {
private static final Class[] parameters = new Class[]{URL.class};
public static void addFile(String s) throws IOException {
File f = new File(s);
addFile(f);
}//end method
public static void addFile(File f) throws IOException {
addURL(f.toURL());
}//end method
public static void addURL(URL u) throws IOException {
URLClassLoader sysloader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class sysclass = URLClassLoader.class;
try {
Method method = sysclass.getDeclaredMethod("addURL", parameters);
method.setAccessible(true);
method.invoke(sysloader, new Object[]{u});
} catch (Throwable t) {
t.printStackTrace();
throw new IOException("Error, could not add URL to system classloader");
}//end try catch
}//end method
}//end class
보호 된 메서드에 액세스하려면 리플렉션이 필요합니다 addURL
. SecurityManager가 있으면 실패 할 수 있습니다.
크기를 위해 이것을 시도하십시오.
private static void addSoftwareLibrary(File file) throws Exception {
Method method = URLClassLoader.class.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(ClassLoader.getSystemClassLoader(), new Object[]{file.toURI().toURL()});
}
This edits the system class loader to include the given library jar. It is pretty ugly, but it works.
The way I have done this is by using my own class loader
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
DynamicURLClassLoader dynalLoader = new DynamicURLClassLoader(urlClassLoader);
And create the following class:
public class DynamicURLClassLoader extends URLClassLoader {
public DynamicURLClassLoader(URLClassLoader classLoader) {
super(classLoader.getURLs());
}
@Override
public void addURL(URL url) {
super.addURL(url);
}
}
Works without any reflection
You coud try java.net.URLClassloader with the url of the folder/jar where your updated class resides and use it instead of the default classloader when creating a new thread.
yes, you can. it will need to be in its package structure in a separate directory from the rest of your compiled code if you want to isolate it. you will then just put its base dir in the front of the classpath on the command line.
Yes I believe it's possible but you might have to implement your own classloader. I have never done it but that is the path I would probably look at.
My solution:
File jarToAdd = new File("/path/to/file");
new URLClassLoader(((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs()) {
@Override
public void addURL(URL url) {
super.addURL(url);
}
}.addURL(jarToAdd.toURI().toURL());
참고URL : https://stackoverflow.com/questions/1010919/adding-files-to-java-classpath-at-runtime
'IT TIP' 카테고리의 다른 글
C ++ 언어 디자이너가 키워드를 계속 재사용하는 이유는 무엇입니까? (0) | 2020.11.24 |
---|---|
Eclipse 용 Visual Studio의 키 바인딩 (0) | 2020.11.24 |
간격 목록에서 간격 겹침을 검색 하시겠습니까? (0) | 2020.11.24 |
불변의 numpy 배열? (0) | 2020.11.24 |
dragenter 및 dragover 이벤트에서 드래그되는 항목 확인 (0) | 2020.11.24 |