找不到classLoader.getResourceAsStream的文件

时间:2015-03-10 12:14:58

标签: java maven file-io filenotfoundexception

我想在运行时读取项目中的.properties文件。 项目结构如屏幕截图所示。

我的代码如下

public void getDefaultConfigurationView() {
    Log.i(TAG, "Problem starts v40.0");
    Properties properties = new Properties();
    try {
        ClassLoader classLoader = context.getClassLoader();
        System.out.println(classLoader.toString());
        if (classLoader != null) {
            Log.i(TAG, "classLoader is not null");
            InputStream inputStream = classLoader.getResourceAsStream("twitter4j.properties");
            if (inputStream != null) {
                properties.load(inputStream);
            } else {
             throw new FileNotFoundException("twitter4j property file not found");
            }
        } else {
            Log.i(TAG, "classLoader is null");
        }

        System.out.println("end of read");     

    } catch (Exception e) {
        e.printStackTrace();
    }

}

此方法无法读取twitter4j.properties文件,该文件显然位于maven生成的jar中。 堆栈跟踪如下:

03-10 20:10:54.111: I/ambientTwitterNewRuntime(25859): Problem starts v40.0
03-10 20:10:54.111: I/System.out(25859):     dalvik.system.PathClassLoader[dexPath=/data/app/org.ambientdynamix.core-1.apk,libraryPath=/data/app-lib/org.ambientdynamix.core-1]
03-10 20:10:54.111: I/ambientTwitterNewRuntime(25859): classLoader is not null
03-10 20:10:54.111: W/System.err(25859): java.io.FileNotFoundException: twitter4j property file not found
03-10 20:10:54.111: W/System.err(25859):    at org.ambientdynamix.contextplugins.ambientTwitterNew.ambientTwitterNewRuntime.getDefaultConfigurationView(ambientTwitterNewRuntime.java:91)
03-10 20:10:54.111: W/System.err(25859):    at org.ambientdynamix.core.DynamixService.doContextPluginConfigurationView(DynamixService.java:2382)
03-10 20:10:54.111: W/System.err(25859):    at org.ambientdynamix.core.DynamixService.openContextPluginConfigurationForFramework(DynamixService.java:2356)
03-10 20:10:54.111: W/System.err(25859):    at org.ambientdynamix.ui.adapters.InstalledPluginsListAdapter$4.onClick(InstalledPluginsListAdapter.java:212)
03-10 20:10:54.111: W/System.err(25859):    at android.view.View.performClick(View.java:4633)
03-10 20:10:54.111: W/System.err(25859):    at android.view.View$PerformClick.run(View.java:19330)
03-10 20:10:54.111: W/System.err(25859):    at android.os.Handler.handleCallback(Handler.java:733)
03-10 20:10:54.111: W/System.err(25859):    at android.os.Handler.dispatchMessage(Handler.java:95)
03-10 20:10:54.111: W/System.err(25859):    at android.os.Looper.loop(Looper.java:157)
03-10 20:10:54.111: W/System.err(25859):    at android.app.ActivityThread.main(ActivityThread.java:5356)
03-10 20:10:54.111: W/System.err(25859):    at java.lang.reflect.Method.invokeNative(Native Method)
03-10 20:10:54.111: W/System.err(25859):    at java.lang.reflect.Method.invoke(Method.java:515)
03-10 20:10:54.111: W/System.err(25859):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
03-10 20:10:54.111: W/System.err(25859):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
03-10 20:10:54.111: W/System.err(25859):    at dalvik.system.NativeStart.main(Native Method)

我的maven pom文件包含这个。

<resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
        </resource>
    </resources>

project structure

0 个答案:

没有答案