Link to home
Start Free TrialLog in
Avatar of Knight_G
Knight_G

asked on

HELP - how to get the system directory?

how to get the system directory (%systemroot%)?

thanks
Avatar of kotan
kotan
Flag of Malaysia image

when running the java, you need to define

java -Dsystem.root=%SYSTEMROOT% classname

so in the program,
System.getProperty("system.root");
Avatar of Knight_G
Knight_G

ASKER

ok, it works.
but is there any other method that i can retrieve it during runtime?
because i'm will be using exe4j to convert my app to exe and the VM parameter provided by exe4j doesn't support % sign(it will convert to actual '%'). thanks
ASKER CERTIFIED SOLUTION
Avatar of kotan
kotan
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oops, in  Windows 95/98
String[] cmd = {"command", "/c", "set"};
thanx, but it is windows version dependent, my situation is i  don't know what version of windows the user currently using, then error will occur if i simply use cmd and command. but i change the code as the following to suit with this situation.  thanks for your idea.

Properties prop;
Process process;

try {
      String [] cmd1 = {"command", "/c", "set"};
      process = Runtime.getRuntime().exec(cmd1);
      prop = new Properties();
      prop.load( process.getInputStream());

      System.out.println("system.root: " + prop.getProperty("SystemRoot"));

}

catch(Exception e) {
      try {
            String [] cmd2 = {"cmd", "/c", "set"};
            process = Runtime.getRuntime().exec(cmd2);
            prop = new Properties();
            prop.load( process.getInputStream());

            System.out.println("system.root: " + prop.getProperty("SystemRoot"));
      }

      catch(Exception ex) {
            System.out.println("ex="+ex);
      }

}
Here is the way to detect os type.
  System.getProperty("os.name");