2012-10-25 45 views
1

我想實例化一個新的類對象形式從用戶輸入收集的外部位置。程序詢問用戶文件的位置,比如/tmp/MyTestClass.java。然後我會喜歡它來獲取該.java文件,並在程序中使其成爲可用的類。所以我可以調用像MyClass = new MyTestclass()。我一直在環顧四周,似乎無法找到答案或者甚至有可能?任何信息都會有用。動態檢索JUnit類

謝謝!

-----------編輯---------------

我可能一直在想我的問題。這是一個JUnit測試(對不起,應該提到這個之前)。下面是我用來拉入靜態類的一個例子。我希望能夠從用戶輸入中動態提取JUnit測試文件。 testcastjunit是該類的名稱。我需要能夠以編程方式從用戶輸入中獲取課程並運行測試用例。

org.junit.runner.Result result = JUnitCore.runClasses(**testcastjunit.class**); 
      for (Failure failure : result.getFailures()) { 
       System.out.println(failure.toString()); 
      } 
+0

[This](http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html)應該讓你開始。不過你需要一個'class'文件。可以使用JavaCompiler實例從正在運行的Java應用程序編譯Java文件。如果你有一個JDK,那麼調用'ToolProvider.getSystemJavaCompiler()'應該可以得到一個。 – toniedzwiedz

回答

4

如果我明白你,這是你所需要的:

JavaCompiler jCompiler = ToolProvider.getSystemJavaCompiler(); 
List<String> options = Arrays.asList(
          "-d", "./bin/", 
          path+".java"); 
int compilationResult = jCompiler.run(null, null, null, 
       options.toArray(new String[options.size()])); 
if (compilationResult == 0) { 
    mensaje = "Compiled the "+path+" to its .class"; 
    ClassLoader cLoader = ClassLoader.getSystemClassLoader(); 
    try { 
     cLoader.loadClass("THE CLASS"); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 
} else { 
    mensaje = "Couldnt compile."; 
} 

這會爲你工作:

  1. 它得到了Java編譯器編譯的類。
  2. 創建選項,-d是您想要將.class編譯爲一次的位置,第二個是您的.java文件的路徑。
  3. 編譯,如果編譯成功,則加載該類。
  4. 開始使用你的課堂!
+0

+1我坦白我從來沒有聽說過'ToolProvider'。我今天學到了一些東西:) – Bohemian

+0

這並不是我腦海中所想的,如何知道它是非常有用的,我一定會在其他項目中牢記這一點。謝謝。當我獲得足夠的代表哈哈時,我會+1 +1 – Lazadon

+0

我撤銷了我的評論這對我非常有幫助。謝謝哈維爾 – Lazadon

1

感謝Javier的建議,我能夠讓我的程序動態編譯和運行JUnit測試用例。我使用它來運行Selenium IDE導出的.java文件。以下是我完成的示例。希望這可以幫助其他人尋找類似的解決方案。另一個說明我正在使用Eclipse IDE進行開發,快樂編碼!

//the loc and name variables are gathered from user input 
    String fileloc = loc +"/"+ name + ".java"; 
    JavaCompiler jCompiler = ToolProvider.getSystemJavaCompiler(); 
    List<String> options = Arrays.asList("-d", "./bin/",fileloc); 

    int compilationResult = jCompiler.run(null, null, null, 
      options.toArray(new String[options.size()])); 
    if (compilationResult == 0){ 
     //This is the package name exported from selenium IDE exported files 
     File file = new File("./bin/com/example/tests/" + name); 
     URL url = null; 
     try { 
      url = file.toURL(); 
      URL[] urls = {url}; 
      ClassLoader cl = new URLClassLoader(urls); 
      org.junit.runner.Result result = JUnitCore.runClasses(cl.loadClass 
        ("com.example.tests." + name)); 
      for (Failure failure : result.getFailures()) { 
       System.out.println(failure.toString()); 
      }; 
     } catch (MalformedURLException e) { 
      System.out.println("Error with file location (URL)"); 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      System.out.println("Couldn't Not Find Test Class To Load"); 
      e.printStackTrace(); 
     } 
    }else{ 
     System.out.println("Could not Find Java Source File Located in `" + fileloc + "`"); 
     System.exit(1); 
    } 
} 
+1

不客氣! –