java 現在のユーザー名を取得する

java 現在のユーザー名を取得する

javaで、現在のユーザー名を取得する手順を記述してます。

環境

  • OS windows11 home
  • java 17.0.2

手順

現在のユーザー名を取得するには、「System.getProperty」を使用します。

System.getProperty("user.name")

実際に、使用してみます。

public class App {
    public static void main(String[] args) throws Exception {     
        
        System.out.println(System.getProperty("user.name")); // testuser

    }
}

取得されていることが確認できます。