javaで現在プロジェクトのパスを取得する方法

Javaで現在のプロジェクトのパスを取得するコードは以下の通りです。

  1. System.getProperty(“user.dir”)
String projectPath = System.getProperty("user.dir");
System.out.println("当前项目路径:" + projectPath);
  1. クラスローダー
  2. getResource() を取得する
String classPath = getClass().getResource("").getPath();
String projectPath = classPath.substring(0, classPath.indexOf("target"));
System.out.println("当前项目路径:" + projectPath);
  1. Paths.get(“”).toAbsolutePath().toString()
String projectPath = Paths.get("").toAbsolutePath().toString();
System.out.println("当前项目路径:" + projectPath);

用途に応じて、プロジェクト構造によって、上記の方法で現在のプロジェクト パスを取得できます。

广告
広告は10秒後に閉じます。
bannerAds
bannerAds