Wallkan
Wallkan

Reputation: 470

How to obtain OS directory

I,m looking for some method that can let me obtain (in windows) the directory where windows is saved (for example in my PC it will return "C:\windows".

I need it because I have to call this method

public static void openFileWithNotepad(String pathFileTxt) throws InterruptedException, IOException
{
    if(System.getProperty("os.name").toUpperCase().contains("Windows".toUpperCase()))
    {
            String program = "C:/WINDOWS/system32/notepad.exe";
            Process p = Runtime.getRuntime().exec(program + " " + pathFileTxt);
    }
...
}

I want to use some method to switch "C:/WINDOWS" with the OS installation folder, in order to use this program on different pcs.

P.S.: If someone know, I'd like also to know how to use this method on UNIX OSs :)

Thank you for understanding!

Upvotes: 0

Views: 474

Answers (4)

Andrew Thompson
Andrew Thompson

Reputation: 168825

Desktop.getDesktop().open(new File(pathFileTxt));

Works for any file for which there is an associated program, on any OS that supports Java 1.6+. See Desktop.open(File) for details.

Upvotes: 2

Abdullah Jibaly
Abdullah Jibaly

Reputation: 54790

System.getenv("WINDIR") may work for you.

Upvotes: 0

jtahlborn
jtahlborn

Reputation: 53694

i believe this should work:

System.getenv("WINDIR")

also, notepad doesn't tend to exist on unix, so i'm not sure where you are going with that...

Upvotes: 1

Bala R
Bala R

Reputation: 108937

try

System.getenv("windir") 

for windows.

I'm not sure about other OSs.

Upvotes: 0

Related Questions