javaで現在プロジェクトのパスを取得する方法
Javaで現在のプロジェクトのパスを取得するコードは以下の通りです。
- System.getProperty(“user.dir”)
String projectPath = System.getProperty("user.dir");
System.out.println("当前项目路径:" + projectPath);
- クラスローダー
- getResource() を取得する
String classPath = getClass().getResource("").getPath();
String projectPath = classPath.substring(0, classPath.indexOf("target"));
System.out.println("当前项目路径:" + projectPath);
- Paths.get(“”).toAbsolutePath().toString()
String projectPath = Paths.get("").toAbsolutePath().toString();
System.out.println("当前项目路径:" + projectPath);
用途に応じて、プロジェクト構造によって、上記の方法で現在のプロジェクト パスを取得できます。