2017-06-14 45 views
0

我在運行時創建一個功能文件,並在創建文件後嘗試訪問該文件。我遵循了所有給出的方法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()}); 
    } 
+0

你應該看看自定義的類加載器。 –

回答

0

試試這個關於大小。

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()}); 
    } 

該編輯系統類加載器包含指定的文件。

+0

我試過了,沒有幫助(: – user1553680

+0

可以請你分享一下你的代碼 – Akash

+0

我已經更新了我的問題 – user1553680

相關問題