0

Possible Duplicate:
Get login username in java

I am looking for a way to find you the name of the user in the OS in java so I can get the right directory on how to find out whether a there is a missing file on the user's appdata. I am using this code:

`File myFile = new File("C:" + File.separator + "Users" + File.separator, "Matty" + File.separator, "AppData" + File.separator, "Roaming");

System.out.println(myFile.isDirectory());

if (myFile.isDirectory() = true)

{
//This will install the program, but i need to find out whether it is already installed

installer.install()

}`

There must be a code for this as most installers have to know what the user's name is!

Community
  • 1
  • 1
Matthew
  • 185
  • 2
  • 12

1 Answers1

2

Try:

public class OSName
{
    public static void main(String[] args)
    {
        System.out.println("Your username is: " + System.getProperty("user.name"));
    }
}

The relevant docs are here and here.

  • How could I make that a variable? – Matthew Jan 05 '13 at 19:57
  • @Matthew - Look at the docs. `System.getProperty(String key)` returns a `String`. –  Jan 05 '13 at 19:57
  • 1
    The name of the system property with the user's home directory is "user.home". For an overview of the Java system properties see http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html. – Adriaan Koster Jan 05 '13 at 20:14