java中獲取項目路徑
代碼:
package com.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.io.File;
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@RestController
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
@GetMapping("/lujing")
public void getLujing() throws Exception{
//當前項目下路徑
File file = new File("");
String filePath = file.getCanonicalPath();
System.out.println(filePath);
//當前項目下xml文件夾
File file1 = new File("");
String filePath1 = file1.getCanonicalPath()+File.separator+"xml\\";
System.out.println(filePath1);
//獲取類加載的根路徑
File file3 = new File(this.getClass().getResource("/").getPath());
System.out.println(file3);
//獲取當前類的所在工程路徑
File file4 = new File(this.getClass().getResource("").getPath());
System.out.println(file4);
//獲取所有的類路徑 包括jar包的路徑
System.out.println(System.getProperty("java.class.path"));
}
}
粘貼過來的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
項目結構:
輸出結果:
1 2 3 4 5 6 7 8 9 |
|