PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/java_various/env/ReadEnv.java

https://github.com/rrguntaka/Project1
Java | 47 lines | 42 code | 2 blank | 3 comment | 7 complexity | e66f6f0aff9a2867560060e60ce1f12b MD5 | raw file
  1. import java.io.*;
  2. import java.util.*;
  3. public class ReadEnv {
  4. public static Properties getEnvVars() throws Throwable {
  5. Process p = null;
  6. Properties envVars = new Properties();
  7. Runtime r = Runtime.getRuntime();
  8. String OS = System.getProperty("os.name").toLowerCase();
  9. // System.out.println(OS);
  10. if (OS.indexOf("windows 9") > -1) {
  11. p = r.exec( "command.com /c set" );
  12. }
  13. else if ( (OS.indexOf("nt") > -1)
  14. || (OS.indexOf("windows 2000") > -1 )
  15. || (OS.indexOf("windows xp") > -1) ) {
  16. // thanks to JuanFran for the xp fix!
  17. p = r.exec( "cmd.exe /c set" );
  18. }
  19. else {
  20. // our last hope, we assume Unix (thanks to H. Ware for the fix)
  21. p = r.exec( "env" );
  22. }
  23. BufferedReader br = new BufferedReader
  24. ( new InputStreamReader( p.getInputStream() ) );
  25. String line;
  26. while( (line = br.readLine()) != null ) {
  27. int idx = line.indexOf( '=' );
  28. String key = line.substring( 0, idx );
  29. String value = line.substring( idx+1 );
  30. envVars.setProperty( key, value );
  31. System.out.println( key + " = " + value );
  32. }
  33. return envVars;
  34. }
  35. public static void main(String args[]) {
  36. try {
  37. Properties p = ReadEnv.getEnvVars();
  38. System.out.println("\n\nthe current value of Path is : \n\n" +
  39. p.getProperty("Path"));
  40. }
  41. catch (Throwable e) {
  42. e.printStackTrace();
  43. }
  44. }
  45. }