[英]Java JAR files locked by ClassLoader
我正在將WAR提取到一個臨時文件夾中,並將所有類和庫添加到URLClassLoader
。
當我開始將類加載到類路徑中時,JVM鎖定“ WEB-INF / lib /”下的JAR文件。
完成該工作后,我關閉類路徑,調用GC並刪除臨時目錄。
刪除臨時目錄時,可以刪除“ WEB-INF / lib”下除JAR文件之外的所有文件。
我的代碼:
@Override
public void generateRegressionTestClasses(Path fileJARorWarSolution, List<String> canonicalNameClassesToGenerateTests) throws Exception {
Path tempPath = null;
URLClassLoader classLoader = null;
TryTest tryTest = null;
RDP execution = null;
try {
// This extracts the JAR/WAR and prepares the URLClassLoader.
GenFilterResponse response = genFilterClass.runGenFilterClass(path);
classLoader = response.getCl();
tempPath = response.getTempPath();
// Verify that all the Test classes is in the ClassLoader.
// Here is where the JAR files are locked.
for (String s : canonicalNameClassesToGenerateTests) {
try {
classLoader.loadClass(s);
} catch (ClassNotFoundException e) {
throw new CustomException(CustomTexts.ERROR_CLASS_NOT_LOADED + s);
}
}
execution = new RDP();
execution.setClassLoader(classLoader);
TryTest = new TryTest(execution);
tryTest.handle(null);
} finally {
tryTest = null;
execution = null;
if (classLoader != null) {
try {
classLoader.close();
} catch (Exception e) {
//
} finally {
classLoader = null;
System.gc();
}
}
if (tempPath != null) {
FileSystemUtils.deleteRecursively(tempPath.toFile());
}
}
有人知道如何解鎖這些文件嗎?
解決了。
如果我使用這種URI將JAR加載到類加載器中:
new URL("jar:file:" + jar.toAbsolutePath().toString() + "!/").toURI().toURL()
URI示例:
jar:文件:C:/ myJar.jar
關閉ClassLoader后,文件被鎖定。
如果我使用這種URI將JAR加載到類加載器中:
warDirectory.toFile().toURI().toURL()
URI示例:
文件:c:/myJar.jar
關閉類加載器時,可以刪除JAR。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.