Get resources from jar file

I need a jar file to access some files. I know how to do this for BufferedImage , but this does not work for other files. All I want to do is extract some lightning from my can. I created a class folder in eclipse, placed the zip inside and used

  public File getResFile(String name){ return new File(getClass().getResource(name).getFile()); } 

to get an instance of File and extract it. it works fine in eclipse, but as soon as I export it to the jar, it says

 Exception in thread "main" java.io.FileNotFoundException: file:\C:\Users\DeLL\Desktop\BoxcraftClient\ClientInstaller.jar!\client.bxc (The filename, directory name, or volume label syntax is incorrect) at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:220) at java.util.zip.ZipFile.<init>(ZipFile.java:150) at java.util.zip.ZipFile.<init>(ZipFile.java:164) at Launcher.install(Launcher.java:43) at Launcher.main(Launcher.java:33) 

Im working to fix this for about 6 hours and cannot find a solution. Please, help!

0
java eclipse jar zip
Oct 21 '15 at 16:43
source share
4 answers

There is a reason why getResource() returns a URL rather than a File , because the resource may not be a file, and since your code is packaged in a Jar file, it is not a file, but a zip.

The only safe way to read the contents of a resource is as an InputStream either by calling getResourceAsStream() or by calling openStream() in the returned URL .

+2
Oct 21 '15 at 16:49
source share
— -

Use one of these methods from the Class class - getResource (java.lang.String) - getResourceAsStream (java.lang.String)

  this.getClass().getResource(name); this.getClass().getResourceAsStream(name); 

Warning: By default, it downloads the file from the location where this.class is in the package. Therefore, if you use it from the class org.organisation.project.App , then the file must be inside the jar in the directory org/organisation/project . In case the file is located in the root or in another directory inside the flag, use / , from the file name. Like /data/names.json .

+1
Oct 21 '15 at 16:57
source share

first check the path to your path with java System.out.println("classpath is: " + System.getProperty("java.class.path")); to find out if the pathpath file has your jar file.

And then use getclass (). classloader.getResourceAsStream (name). Verify that the returned URL is correct. Call the isFile () method on the URL to check if the URL is really a file. Then call the getFile () method.

+1
Oct 21 '15 at 17:35
source share

Use Spring PathMatchingResourcePatternResolver ;

It will do the trick both to run the package from the IDE and from the file system:

 public List<String> getAllClassesInRunningJar() throws Exception { try { List<String> list = new ArrayList<String>(); // Get all the classes inside the package com.my.package: // This will do the work for both launching the package from an IDE or from the file system: String scannedPackage = "com.my.package.*"; // This is spring - org.springframework.core; use these imports: // import org.springframework.core.io.Resource; // import org.springframework.core.io.support.PathMatchingResourcePatternResolver; PathMatchingResourcePatternResolver scanner = new PathMatchingResourcePatternResolver(); Resource[] resources = scanner.getResources(scannedPackage.replace(".", "/")); for (Resource resource : resources) list.add(resource.getURI().toString()); return list ; } catch (Exception e) { throw new Exception("Failed to get the classes: " + e.getMessage(), e); } } 
+1
Jul 24. '17 at 7:19
source share



All Articles