[英]Java JAR files locked by ClassLoader
I am extracting a WAR in a temporary folder and adding all the classes and libs to a URLClassLoader
. 我正在将WAR提取到一个临时文件夹中,并将所有类和库添加到
URLClassLoader
。
When I start to load the classes into the classpath, the JAR files under "WEB-INF/lib/" are locked by the JVM. 当我开始将类加载到类路径中时,JVM锁定“ WEB-INF / lib /”下的JAR文件。
After doing the job, I close the classpath, call the GC and delete the temporary directory. 完成该工作后,我关闭类路径,调用GC并删除临时目录。
When I am deleting the temporary directory, I can delete all the files except the JAR files under "WEB-INF/lib". 删除临时目录时,可以删除“ WEB-INF / lib”下除JAR文件之外的所有文件。
My code: 我的代码:
@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());
}
}
Anybody knows how to unlock these files? 有人知道如何解锁这些文件吗?
Solved. 解决了。
If I load the JAR into the Class Loader with this kind of URI: 如果我使用这种URI将JAR加载到类加载器中:
new URL("jar:file:" + jar.toAbsolutePath().toString() + "!/").toURI().toURL()
Example URI: URI示例:
jar:file:c:/myJar.jar jar:文件:C:/ myJar.jar
The file is locked after ClassLoader is closed. 关闭ClassLoader后,文件被锁定。
If I load the JAR into the Class Loader with this kind of URI: 如果我使用这种URI将JAR加载到类加载器中:
warDirectory.toFile().toURI().toURL()
Example URI: URI示例:
file:c:/myJar.jar 文件:c:/myJar.jar
I can delete the JAR's when the Classloader is closed. 关闭类加载器时,可以删除JAR。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.