Java Jar在Android中不起作用

时间:2015-10-01 15:13:28

标签: java android maven jar classloader

我写了一个小的Java应用程序,我将其编译成可执行的jar文件。该程序使用在jar文件中编译的.txt文件。要访问jar中的.txt文件,您需要使用ClassLoader.getSystemResourceAsSteam(“”);

以下是jar文件中的方法:

 private ArrayList<String> loadChainRestaurantsList() throws IOException {
        ArrayList chains = new ArrayList();
        InputStream in = ClassLoader.getSystemResourceAsStream("chains.txt");
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));

        String name;
        while((name = reader.readLine()) != null) {
            chains.add(name);
        }

        return chains;
    }

如果我将cmd-line中的jar作为独立的Java程序或Eclipse运行,则此代码可以正常工作。它甚至可以工作,如果我导出为jar然后将其抛入另一个Java程序并使用它,它按预期工作。问题是当我将jar扔进Android项目时。我收到这个错误:

FATAL EXCEPTION: AsyncTask #1
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime: Process: com.jasonjohn.unchainedandroid, PID: 8821
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at android.os.AsyncTask$3.done(AsyncTask.java)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:  Caused by: java.lang.NullPointerException: lock == null
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.io.Reader.<init>(Reader.java:64)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.io.InputStreamReader.<init>(InputStreamReader.java:120)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.io.InputStreamReader.<init>(InputStreamReader.java:57)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at com.jasonjohn.unchainedapi.UnchainedAPI.loadChainRestaurantsList(UnchainedAPI.java:123)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at com.jasonjohn.unchainedapi.UnchainedAPI.getUnchainedRestaurants(UnchainedAPI.java:71)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at com.jasonjohn.unchainedandroid.MainActivity$UnchainedAsync.doInBackground(MainActivity.java:69)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at com.jasonjohn.unchainedandroid.MainActivity$UnchainedAsync.doInBackground(MainActivity.java:62)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at android.os.AsyncTask$2.call(AsyncTask.java)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java) 
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
10-01 10:40:59.650 8821-8879/? E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818) 

我以前从未见过这个错误。谁能解释一下发生了什么?我假设InputStream / InputStreamReader为null,但我不知道如何解决这个问题。

1 个答案:

答案 0 :(得分:1)

您应该使用

getResourceAsStream("/chains.txt")

而不是使用

getSystemResourceAsStream(...).

路径前面的/表示查看jar的根部。否则,它将在与您从类加载器中获取的类相同的包中查找chains.txt。 EG。,如果你这样做:

package com.potter.harry;
...
InputStream is = this.getClass().getResourceAsStream("chains.txt");

引用的文件必须放在“/com/potter/harry/chains.txt” 在zip文件中。