I am extracting a WAR in a temporary folder and adding all the classes and libs to a URLClassLoader
.
When I start to load the classes into the classpath, the JAR files under "WEB-INF/lib/" are locked by the JVM.
After doing the job, I close the classpath, call the GC and delete the temporary directory.
When I am deleting the temporary directory, I can delete all the files except the JAR files under "WEB-INF/lib".
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:
new URL("jar:file:" + jar.toAbsolutePath().toString() + "!/").toURI().toURL()
Example URI:
jar:file:c:/myJar.jar
The file is locked after ClassLoader is closed.
If I load the JAR into the Class Loader with this kind of URI:
warDirectory.toFile().toURI().toURL()
Example URI:
file:c:/myJar.jar
I can delete the JAR's when the Classloader is closed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.