JAVA Gets the instance code for the current project and file path

  • 2021-08-31 08:04:45
  • OfStack

Directly on the code:


 // Path under current project 
  File file = new File("");
  String filePath = file.getCanonicalPath();
  System.out.println(filePath);
 
  // Under the current project xml Folder 
  File file1 = new File("");
  String filePath1 = file1.getCanonicalPath()+File.separator+"xml\\";
  System.out.println(filePath1);
 
  // Gets the root path of the class load 
  File file3 = new File(this.getClass().getResource("/").getPath());
  System.out.println(file3);
 
  // Gets the project path of the current class 
  File file4 = new File(this.getClass().getResource("").getPath());
  System.out.println(file4);
 
  // Get all classpath   Include jar Path of package 
  System.out.println(System.getProperty("java.class.path"));

Output:

1. Path under current project

E:\Work\example

2. xml folder under current project

E:\Work\example\xml

3. Get the root path of the class load

E:\Work\example\out\production\classes

4. Get the project path of the current class

E:\Work\example\out\production\classes\com\demo

5. Get all the ClassPath including the path of the jar package D:\ Java\ jdk1.8.0_65\ jre\ lib\ charsets. jar; D:\ Java\ jdk1.8.0_65\ jre\ lib\ deploy.jar;

Knowledge point supplement:

Java Get the current project file path

Templates or other files for the project are needed at some point in time, and you may need to combine them to get the file path.


String projectPath = System.getProperty( " user.dir " );
System.out.println( " projectPath== "  + projectPath);

Related articles: