簡體   English   中英

運行的jar中來自classpath目錄的文件列表

[英]File list from classpath directory in running jar

我想從類路徑中加載所有資源包屬性,以便可以顯示支持的語言。 我從這里獲得了參考,並嘗試了第一種解決方案。 當我從Eclipse運行代碼時,它可以工作。 但是,當我創建可執行jar文件時,它無法讀取文件。 我不知道為什么從命令java -jar AppName.jar運行時行為會有所不同

我的代碼是:

public static List<String> getResourceFiles(String path) throws IOException
{
    List<String> filenames = new ArrayList<>();

    InputStream in = getResourceAsStream(path);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    System.out.println("br = " + br.readLine());
    String resource;
    while ((resource = br.readLine()) != null)
    {
        filenames.add(resource);
    }

    return filenames;
}

private static InputStream getResourceAsStream(String resource)
{
    final InputStream in = getContextClassLoader().getResourceAsStream(resource);
    System.out.println("input stream = " + in);

    return in == null ? FileUtil.class.getResourceAsStream(resource) : in;
}

private static ClassLoader getContextClassLoader()
{
    return Thread.currentThread().getContextClassLoader();
}

在這里,我注意到從命令運行時InputStream為null,但是從Eclipse運行時InputStream不為null。

如何解決此問題,以便從命令運行時也可以讀取資源文件?

我找到了解決方案。 下面的代碼為我工作:

public static String[] getResourceListing(Class clazz, String path) throws URISyntaxException, IOException
{
    URL dirURL = clazz.getClassLoader().getResource(path);
    if (dirURL != null && dirURL.getProtocol().equals("file"))
    {
        /* A file path: easy enough */
        return new File(dirURL.toURI()).list();
    }

    if (dirURL == null)
    {
        /*
         * In case of a jar file, we can't actually find a directory. Have to assume the
         * same jar as clazz.
         */
        String me = clazz.getName().replace(".", "/") + ".class";
        dirURL = clazz.getClassLoader().getResource(me);
    }

    if (dirURL.getProtocol().equals("jar"))
    {
        /* A JAR path */
        String jarPath = dirURL.getPath().substring(5, dirURL.getPath().indexOf("!")); // strip out only the JAR file
        JarFile jar = new JarFile(URLDecoder.decode(jarPath, "UTF-8"));
        Enumeration<JarEntry> entries = jar.entries(); // gives ALL entries in jar
        Set<String> result = new HashSet<String>(); // avoid duplicates in case it is a subdirectory
        while (entries.hasMoreElements())
        {
            String name = entries.nextElement().getName();
            if (name.startsWith(path))
            { // filter according to the path
                String entry = name.substring(path.length());
                int checkSubdir = entry.indexOf("/");
                if (checkSubdir >= 0)
                {
                    // if it is a subdirectory, we just return the directory name
                    entry = entry.substring(0, checkSubdir);
                }
                result.add(entry);
            }
        }
        return result.toArray(new String[result.size()]);
    }

    throw new UnsupportedOperationException("Cannot list files for URL " + dirURL);
}

我認為您可以將應用程序路徑添加到系統環境(類路徑),如果無法解決問題,請在初始化文件流並修復它之前嘗試打印真實路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM