Learn to read a file from classpath in Java. The file can be present at the root of class path location or in any relative sub-directory.
1. Adding files in classpath
Jul 18, 2019 Path toFile method in Java with Examples. The toFile method of java.nio.file.Path interface used to return a java.io.File object representing this path object. If this Path is associated with the default provider, then this method returns a java.io.File object constructed with the String representation of this path. Java – Read a file from resources folder. In Java, we can use getResourceAsStream or getResource to read a file or multiple files from a resources folder or root of the classpath. The getResourceAsStream method returns an InputStream. The getResource method returns an URL and normally convert it to a File; Not working in JAR file.
The classpath of an application generally contains following locations:
- Project’s root directory
/src/main/resources
- Any location whose files are copied to
/classes
folder after project build runs.
If the file is present inside a jar file, add the jar file in the projects lib
folder or add the jar in projects classpath.
The important thing to validate is that the file is copied into final build output of the project i.e. jar/war file or ear file.
2. Reading a file from classpath
Reading a file from classpath is simple. We have to get the reference of system classloader for the class which is trying to read the file.
This classloader obviously knows the other paths for the application. Once we have the File
reference, we can use a number of ways to read the file.
Where the file contents are:
3. File paths
The file name can be given in two ways i.e. root directory paths and full paths.
fileName.txt
– searches the file in the root path of the build folder (e.g./bin
,/build
,/WEB-INF/classes
etc).com/howtodoinjava/io/demo.txt
– represent the full path of the file within the build directory. In this case actual file is present in location<project_root>/bin/com/howtodoinjava/io/demo.txt
.
Drop me your questions related to reading a file from the classpath in Java.
Happy Learning !!
Some Java applications require to define JAVA_HOME and Java path. Keep reading and I’ll explain how to do it in Windows 10.
How to set Java Home
You need to do the following to set JAVA_HOME:
File Path In For Java Download
Search for Advanced System Settings and launch it:
File Path In Java
Go to Environment Variables:
Press “New” button in “System variables” section:
Type name “JAVA_HOME” and value – your path to Java, in my case, it’s C:Program FilesJavajre-9.0.4 and press “OK“.
Done.
How to set Java path
Find “Path” system variable and press “Edit“.
Press “New” and type “%JAVA_HOME%bin” to add Java path.
Done.
Check if JAVA_HOME variable is defined
We set Java home and Java path variables, let’s check is it really working.
File Path In Java Project
Open command prompt.
We can check Java home variable by typing “echo %JAVA_HOME%“.
We can verify the version of Java as well using “java -version” command.
File Path In For Javascript
If everything is OK your output like this: