如何从.jar加载文件夹?

前端之家收集整理的这篇文章主要介绍了如何从.jar加载文件夹?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好.所以我有一个非常简单的问题:我希望能够从正在运行的.jar文件中加载资源(整个文件夹),但我无法让它工作.这是我尝试过的(如果类名是“myClass”,文件夹被称为“myFolder”),但它总是抛出NullPointerException:
  1. URL folderURL = myClass.class.getClassLoader().getResource("myFolder/");
  2. String folderPath = folderURL.getPath();
  3. File myFolder = new File(folderPath);

在创建“myFolder”之前,总是抛出NullPointerException.

更多信息:我必须从静态上下文访问该文件夹.正在访问该文件夹的类与文件夹本身所在的目录不在同一目录中.(该文件夹位于jar内的根目录中,该类是几个子包.)

有没有人能解决我的问题?对不起,如果我使用了错误的术语:P,但您可以做任何事情来帮助我们.

解决方法

这没办法.您正在尝试从JAR内的资源创建File对象.这不会发生.加载资源的最佳方法是将一个包文件夹作为资源文件夹,然后在其中创建一个Resources.jar,将资源转储到同一目录中,然后在其他目录中使用Resources.class.getResourceAsStream(resFileName) Java类文件.

如果你需要“暴力破解”由getResource(..)给出的URL指向的JAR目录中的子文件,请使用以下内容(尽管这有点像黑客!).它也适用于普通的文件系统:

  1. /**
  2. * List directory contents for a resource folder. Not recursive.
  3. * This is basically a brute-force implementation.
  4. * Works for regular files and also JARs.
  5. *
  6. * @author Greg Briggs
  7. * @param clazz Any java class that lives in the same place as the resources you want.
  8. * @param path Should end with "/",but not start with one.
  9. * @return Just the name of each member item,not the full paths.
  10. * @throws URISyntaxException
  11. * @throws IOException
  12. */
  13. String[] getResourceListing(Class clazz,String path) throws URISyntaxException,IOException {
  14. URL dirURL = clazz.getClassLoader().getResource(path);
  15. if (dirURL != null && dirURL.getProtocol().equals("file")) {
  16. /* A file path: easy enough */
  17. return new File(dirURL.toURI()).list();
  18. }
  19.  
  20. if (dirURL == null) {
  21. /*
  22. * In case of a jar file,we can't actually find a directory.
  23. * Have to assume the same jar as clazz.
  24. */
  25. String me = clazz.getName().replace(".","/")+".class";
  26. dirURL = clazz.getClassLoader().getResource(me);
  27. }
  28.  
  29. if (dirURL.getProtocol().equals("jar")) {
  30. /* A JAR path */
  31. String jarPath = dirURL.getPath().substring(5,dirURL.getPath().indexOf("!")); //strip out only the JAR file
  32. JarFile jar = new JarFile(URLDecoder.decode(jarPath,"UTF-8"));
  33. Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
  34. Set<String> result = new HashSet<String>(); //avoid duplicates in case it is a subdirectory
  35. while(entries.hasMoreElements()) {
  36. String name = entries.nextElement().getName();
  37. if (name.startsWith(path)) { //filter according to the path
  38. String entry = name.substring(path.length());
  39. int checkSubdir = entry.indexOf("/");
  40. if (checkSubdir >= 0) {
  41. // if it is a subdirectory,we just return the directory name
  42. entry = entry.substring(0,checkSubdir);
  43. }
  44. result.add(entry);
  45. }
  46. }
  47. return result.toArray(new String[result.size()]);
  48. }
  49.  
  50. throw new UnsupportedOperationException("Cannot list files for URL "+dirURL);
  51. }

然后,您可以修改getResource(..)给出的URL并将文件附加到末尾,并将这些URL传递给getResourceAsStream(..),以备加载.如果您不理解这一点,则需要阅读类加载.

猜你在找的Java相关文章