Get file in resource folder in Java

I want to read a file in my resources folder in my Java project. I used the following code for this

MyClass.class.getResource("/myFile.xsd").getPath(); 

And I wanted to check the file path. But he gives the next path

 file:/home/malintha/.m2/repository/org/wso2/carbon/automation/org.wso2.carbon.automation.engine/4.2.0-SNAPSHOT/org.wso2.carbon.automation.engine-4.2.0-SNAPSHOT.jar!/myFile.xsd 

I get the file path depending on the maven repository and it does not receive the file. How can i do this?

+7
source share
6 answers

You need to specify the path to your res folder.

 MyClass.class.getResource("/res/path/to/the/file/myFile.xsd").getPath(); 
+4
source

Is your resource directory in the classpath?

You do not include the resource directory in your path:

 MyClass.class.getResource("/${YOUR_RES_DIR_HERE}/myFile.xsd").getPath(); 
+3
source

A reliable way to create a file instance from the resource folder is to copy the resource as a stream to a temporary file (the temp file will be deleted when you exit the JVM):

 public static File getResourceAsFile(String resourcePath) { try { InputStream in = ClassLoader.getSystemClassLoader().getResourceAsStream(resourcePath); if (in == null) { return null; } File tempFile = File.createTempFile(String.valueOf(in.hashCode()), ".tmp"); tempFile.deleteOnExit(); try (FileOutputStream out = new FileOutputStream(tempFile)) { //copy stream byte[] buffer = new byte[1024]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } return tempFile; } catch (IOException e) { e.printStackTrace(); return null; } } 
+1
source

Unable to access resources of other maven modules. Therefore, you need to provide your resource myFile.xsd in the src/main/resources or src/test/resources folder.

0
source

The path is correct, although not in the file system, but inside the jar. That is, because the bank ran. A resource will never be a guarantee of being a file.

However, if you do not want to use resources, you can use the zip file system. However, Files.copy would be enough to copy the file outside the banks. Changing the file inside the jar is a bad idea. It is better to use the resource as a "template" to make an initial copy in the user's directory (subdirectory) ( System.getProperty("user.home") ).

0
source

In the maven project, let's assume that we have a file called "config.cnf" and its location is lower.

 /src /main /resources /conf config.cnf 

In the IDE (Eclipse), I access this file using the ClassLoader.getResource (..) method, but if I run this application using jar, I always encounter the exception β€œFile not found”. Finally, I wrote a file access method by looking at where the application works.

 public static File getResourceFile(String relativePath) { File file = null; URL location = <Class>.class.getProtectionDomain().getCodeSource().getLocation(); String codeLoaction = location.toString(); try{ if (codeLocation.endsWith(".jar"){ //Call from jar Path path = Paths.get(location.toURI()).resolve("../classes/" + relativePath).normalize(); file = path.toFile(); }else{ //Call from IDE file = new File(<Class>.class.getClassLoader().getResource(relativePath).getPath()); } }catch(URISyntaxException ex){ ex.printStackTrace(); } return file; } 

If you call this method by sending the "conf / config.conf" parameter, you get access to this file from both the jar and the IDE.

0
source

Source: https://habr.com/ru/post/955017/


All Articles