2011-09-30 48 views
2

我使用sbt 0.11和Scala 2.9.1(它似乎在同一線程中評估REPL行)。在我build.sbt我:如何讓應用程序資源可以訪問控制檯的initialCommands?

initialCommands in console := """ 
println(Thread.currentThread) 
println(Thread.currentThread.getContextClassLoader) 
ru.circumflex.orm.Context.get() // reads my src/main/resources/cx.properties 
""" 

Context.get()加載資源,:

val bundle = ResourceBundle.getBundle(
    "cx", Locale.getDefault, Thread.currentThread.getContextClassLoader) 

這導致錯誤(REPL顯示標準輸出/標準錯誤後緩衝了自己的輸出):

> console 
[info] No CoffeeScripts to compile 
[info] Starting scala interpreter... 
[info] 
Thread[run-main,5,trap.exit] 
[email protected] 
14:17:44.003 [run-main] ERROR ru.circumflex.core - Could not read configuration parameters from cx.properties. 
res0: ru.circumflex.core.Context = ctx() 
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_26). 
Type in expressions to have them evaluated. 
Type :help for more information. 

scala> Thread.currentThread 
res1: java.lang.Thread = Thread[run-main,5,trap.exit] 

scala> Thread.currentThread.getContextClassLoader 
res2: java.lang.ClassLoader = [email protected] 

刪除最後initialCommand線和在REPL結果中沒有錯誤運行它,因爲到那個時候資源是可見的。

任何提示如何處理這個問題,並使我的應用程序的資源訪問initialCommands?

回答

1

發佈後不久發現了答案。只需將此行添加到initialCommands即可:

Thread.currentThread.setContextClassLoader(getClass.getClassLoader) 
相關問題