我在運行時創建一個功能文件,並在創建文件後嘗試訪問該文件。我遵循了所有給出的方法here。如何在運行時將文件添加到Java Classpath?
1. Is there a way to refresh the project at runtime ?
2. Is there any other way to load the file in classpath ?
[編輯]添加示例代碼
public static void createFeatureFile(String featurePath) throws IOException{
FileWriter writer = new FileWriter(featurePath);
writer.append("Feature: Test ");
writer.append("\n");
writer.append("\n");
writer.append("@test");
writer.append("\n");
writer.append("Scenario : add fruits");
writer.append("\n");
writer.append("Given I have 1 apple and 1 orange");
writer.append("\n");
writer.append("Then I add both"");
writer.append("\n");
writer.flush();
writer.close();
File file =new File(featurePath);
addFileAtRunTime(file);
}
private static void addFileAtRunTime(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()});
}
你應該看看自定義的類加載器。 –