a.p. patel
a.p. patel

Reputation: 113

System.getProperty("user.name") returns "?"

I am trying to use System.getProperty("user.name") in my Java Code to get the username for the web services. Somehow it is returning "?" (Question Mark)

What can be the main issue?

If someone calls services do we need to pass the username from the service instead of using system properties?

Upvotes: 0

Views: 1157

Answers (1)

Thomas Kläger
Thomas Kläger

Reputation: 21435

Java has to read the name user it is currently running under from the OS environment. If the OS environment doesn't provide a name for that user, it sets the user.name property to "?" on Unix-like systems and to "unknown" on Windows systems.

The code fragment on Unix systems is:

    struct passwd *pwent = getpwuid(getuid());
    sprops.user_name = pwent ? strdup(pwent->pw_name) : "?";

and on Windows systems it is:

    // ... various variants to try to read the user name
    // followed by
        sprops.user_name = (uname != NULL) ? uname : L"unknown";

If your code runs in a container, the container defines the uid that is used to run your code.

If the system that runs the container doesn't have a user name for that uid (or if the system or the container runtime doesn't allow to access the user name from within the container) then this is what you will see.

Upvotes: 4

Related Questions