2016-02-26 40 views
0

我正在開發一個JavaFX項目,我的項目結構如下。使用谷歌反射來加載資源文件

This is my Image

我想用谷歌Refletions加載在/src/main/java/windchimes.ui/WindChimesApplication.java/src/main/java/resources/view/primary*fxml文件。我嘗試使用下面的代碼爲它

Reflections reflections = new Reflections("windchimes.ui",new ResourcesScanner()); 
Set<String> scenePaths = reflections.getResources(Pattern.compile(".*fxml")); 

當我運行上面的代碼片段,java的拋出以下異常

Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:819) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95) Caused by: java.lang.NoSuchMethodError: com.google.common.cache.CacheBuilder.maximumSize(I)Lcom/google/common/cache/CacheBuilder; at org.reflections.adapters.JavassistAdapter.(JavassistAdapter.java:28) at org.reflections.util.ConfigurationBuilder.(ConfigurationBuilder.java:41) at org.reflections.Reflections$1.(Reflections.java:134) at org.reflections.Reflections.(Reflections.java:134) at org.reflections.Reflections.(Reflections.java:117) at windchimes.ui.WindChimesApplication.(WindChimesApplication.java:27) ... 10 more

我相信,我沒有按照我的使用情況下創建的反思對象。有人可以幫我嗎 ?

回答

1

你需要指定包前綴,我相信你正在尋找.fxml文件。此代碼應該可以工作。你可以試試嗎?

Reflections reflections = new Reflections(new ConfigurationBuilder().setScanners(new ResourcesScanner()) 
      .setUrls(ClasspathHelper.forPackage("windchimes.ui"))); 
    Set<String> res = reflections.getResources(Pattern.compile(".*\\.fxml"));