• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do I get %SyaytemRoot% from a java code?  RSS feed

 
garfild Baram
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I need to retrieve SystemRoot variable value withing a java class.
I couldnt find a way to do it.
Can you help me here?
Thanks
Yossi
 
Michael Duffy
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by garfild Baram:
Hi,
I need to retrieve SystemRoot variable value withing a java class.
I couldnt find a way to do it.
Can you help me here?
Thanks
Yossi


System root? You mean "c:" for windows or "/usr/bin" for *nix?

The better question is: why do you think you need it? Perhaps you can write your app in such a way that you aren't so dependent on file paths.
 
garfild Baram
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey,
I need it to insert my.ini configuration file for mysql in the SystemRoot directory, which is different for win2k and Xp.
I found a way:

String s = null;

try
{
Process pr = Runtime.getRuntime().exec("CMD /c \"echo %systemroot%\"");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
if ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
catch (Exception e)
{
System.err.println ("Error ....");
}
}

Thaks any way

Yossi
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Java 1.5, you can retrieve environment variables with System.getEnv():
 
Mahadevan Gorti SS
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Yoddi,
The example code you submitted may have some issues
[[[
String s = null;

try
{
Process pr = Runtime.getRuntime().exec("CMD /c \"echo %systemroot%\"");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(pr.getInputStream()));
if ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
catch (Exception e)
{
System.err.println ("Error ....");
}
}
]]]

One has to read Exec'd process output/error stream as well -- incase any error/etc, in separate thread to make this exec-stuff work properly all the times.


Another simple solution (instead of costly exec) is use to System.getenv(EN_VAR). This method is available since 1.2 onwards(but deprecated till JDK 1.5).So to reduce no of deprecated warnings in your code(for pre JDK 1.4 code), pass it thru a wrapper-class/method . By using wrapper you will get deprecated warning only once for compilation.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!