Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are using Java 1.5, you can retrieve environment variables with System.getEnv():
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic