This one works for me:
return Path.of(ClassLoader.getSystemResource(resourceName).toURI());
Answer from keyoxy on Stack OverflowOracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Path.html
Path (Java Platform SE 8 )
April 21, 2026 - java.nio.file · All Superinterfaces: Comparable<Path>, Iterable<Path>, Watchable · public interface Path extends Comparable<Path>, Iterable<Path>, Watchable · An object that may be used to locate a file in a file system. It will typically represent a system dependent file path.
Oracle
docs.oracle.com › javase › 8 › docs › api › java › nio › file › Paths.html
Paths (Java Platform SE 8 )
April 21, 2026 - Converts the given URI to a Path object. This method iterates over the installed providers to locate the provider that is identified by the URI scheme of the given URI. URI schemes are compared without regard to case. If the provider is found then its getPath method is invoked to convert the URI.
Videos
07:42
Creating And Reading Files with java.nio.Path | How To Work With ...
21:16
Read from file in Java with java.nio Files and Paths - YouTube
28:47
Java I/O File Handling - Locating the Resource: File (Java IO) ...
51:25
Java Fundamentals - Lesson 64 - java.nio.file.Files vs. java.io.File ...
07:09
144 - Utility Classes - NIO pt 01 - Path, Paths, Files pt 01 - YouTube
Baeldung
baeldung.com › home › java › java io › java – path vs file
Java – Path vs File | Baeldung
April 20, 2024 - Since the very first versions, Java has delivered its own java.io package, which contains nearly every class we might ever need to perform input and output operations. The File class is an abstract representation of file and directory pathnames: ... Instances of the File class are immutable – once created, the abstract pathname represented by this object will never change. The Path class forms part of the NIO2 update, which came to Java with version 7.
Java
download.java.net › java › early_access › panama › docs › api › java.base › java › nio › file › Paths.html
Paths (Java SE 19 & JDK 19 [build 1])
This method simply invokes Path.of(URI) with the given parameter. ... IllegalArgumentException - if preconditions on the uri parameter do not hold. The format of the URI is provider specific. FileSystemNotFoundException - The file system, identified by the URI, does not exist and cannot be created automatically, or the provider identified by the URI's scheme component is not installed
Medium
medium.com › @AlexanderObregon › javas-paths-get-method-explained-9586c13f2c5c
Java’s Paths.get() Method Explained | Medium
September 8, 2024 - Also, reserved characters and file naming conventions differ, which can lead to compatibility issues. By using Paths.get(), Java abstracts these differences, allowing us to focus on logic rather than platform-specific quirks, making it a important tool for making sure that file handling code works reliably across all environments.
Top answer 1 of 8
269
This one works for me:
return Path.of(ClassLoader.getSystemResource(resourceName).toURI());
2 of 8
31
Guessing that what you want to do, is call Files.lines(...) on a resource that comes from the classpath - possibly from within a jar.
Since Oracle convoluted the notion of when a Path is a Path by not making getResource return a usable path if it resides in a jar file, what you need to do is something like this:
Stream<String> stream = new BufferedReader(new InputStreamReader(ClassLoader.getSystemResourceAsStream("/filename.txt"))).lines();
TutorialsPoint
tutorialspoint.com › java_nio › java_nio_path.htm
Java NIO - Path
As name suggests Path is the particular location of an entity such as file or a directory in a file system so that one can search and access it at that particular location. Technically in terms of Java, Path is an interface which is introduced in Java NIO file package during Java version 7,and is the representation of location in particular file system...
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › nio › file › Path.html
Path (Java SE 11 & JDK 11 )
January 20, 2026 - Package java.nio.file · All Superinterfaces: Comparable<Path>, Iterable<Path>, Watchable · public interface Path extends Comparable<Path>, Iterable<Path>, Watchable · An object that may be used to locate a file in a file system. It will typically represent a system dependent file path.
Android Developers
developer.android.com › sdk › api_diff › 34 › changes › java.nio.file.Path
java.nio.file.Path
JDiff is a Javadoc doclet which generates an HTML report of all the packages, classes, constructors, methods, and fields which have been removed, added or changed in any way, including their documentation, when two APIs are compared.
Maven Central
central.sonatype.com
Maven Central
Official search by the maintainers of Maven Central Repository.
University of Edinburgh
inf.ed.ac.uk › teaching › courses › inf1 › op › 2020 › labs › NIO.html
Java File I/O (NIO.2)
Each of the Paths in these result lists will be relative to their respective directory. For example if oldDir is '/a/b', and the result list contains the file '/a/b/c/d', then the list will return that path as 'c/d'. An automated test has been created for this exercise: CompareDirectoriesTest.java.
Neo4j
neo4j.com › neo4j graph intelligence platform
Neo4j Graph Intelligence Platform
1 month ago - Connect data as it's stored with Neo4j. Perform powerful, complex queries at scale and speed with our graph data platform.