Assuming that the resource file is placed under the Src/main/resources Resource folder of the MAVEN project, the source file is placed under src/main/java/, then the Java folder and the resources folder are classpath real locations at runtime, if
There is a file located in Src/main/resources/test.txt
There is a class in Src/main/java/com/qunar/myclass.java
file_name = "Test.txt"
By following the code
MyClass.class.getClassLoder (). GetResource (file_name). GetPath ();
You can get the file path directly
Classpath can be obtained by using the following code
MyClass.class.getClassLoder (). GetResource (""). GetPath ();
There is also a way to get the resource address by using the relative path through the load path of the current class
MyClass.class.getResource (file_name). GetPath ()
Then this is problematic because the path at this time is relative to the MyClass class at run time, Test.txt and MyClass are not at one level, because there are two packages before MyClass
Can be resolved as follows
MyClass.class.getResource (File.separator + "file_name"). GetPath (); This method is equivalent to using the absolute run-time path
MyClass.class.getResource (".." + File.separator + ":" + File.separator + file_name). GetPath (); This method is equivalent to using a relative MyClass run-time path