How to learn from Jar load folder?

OK So I have a very simple question: I want to be able to start from the running Jar file, but I can't make it work This is what I tried (if the class name is "MyClass" and the folder is called "myfolder"), but it always throws NullPointerException:

URL folderURL = myClass.class.getClassLoader().getResource("myFolder/");
String folderPath = folderURL.getPath();
File myFolder = new File(folderPath);

NullPointerException is always thrown before creating "myfolder"

More information: I must access this folder from a static context The class accessing the folder is not in the same directory as the folder itself (this folder is located in the root directory of jar, and this class is several sub packages.)

Can anyone solve my problem? Sorry, if I use the wrong term: P, but you can do anything to help us

Solution

There's no way You are trying to create a file object from a resource within a jar It won't happen The best way to load resources is to use a package folder as a resource folder, and then create a resources Jar, dump the resources to the same directory, and then use resources. Jar in other directories class. Getresourceasstream (resfilename) Java class file

If you need "brute force cracking" by getresource (..) The URL given points to a sub file in the jar directory, please use the following (although it's a bit like a hacker!) It also applies to normal file systems:

/**
   * List directory contents for a resource folder. Not recursive.
   * This is basically a brute-force implementation.
   * Works for regular files and also JARs.
   * 
   * @author Greg Briggs
   * @param clazz Any java class that lives in the same place as the resources you want.
   * @param path Should end with "/",but not start with one.
   * @return Just the name of each member item,not the full paths.
   * @throws URISyntaxException 
   * @throws IOException 
   */
  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);
  }

Then, you can modify getresource (..) And append the file to the end, and pass these URLs to getresourceasstream (..), For loading If you don't understand this, you need to read class loading

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>