A quick-starting tool to view java system properties


/ Published in: Java
Save to your folder(s)

current function

* list all system properties
* list a specific property


Copy this code and paste it in your HTML
  1. import java.util.Properties;
  2. import java.util.Scanner;
  3.  
  4. public class SysPropsTool {
  5. public static final String QUIT = "/Q";
  6. public static final String QUIT_ = "/QUIT";
  7. public static final String HELP = "/?";
  8. public static final String HELP_ = "/H";
  9. public static final String HELP__ = "/HELP";
  10. public static final String LIST = "/LS";
  11. public static final String LIST_ = "/LIST";
  12. public static final String LISTALL = "/ALL";
  13.  
  14. public static final String PROPS = "props >>>";
  15.  
  16. static Properties props = System.getProperties();
  17. public static void main (String[] args) {
  18. //props = System.getProperties();
  19. usageInfo();
  20.  
  21. Scanner sc = new Scanner(System.in);
  22.  
  23.  
  24. while (true) {
  25. System.out.print(PROPS);
  26. try {
  27. String line = sc.nextLine();
  28.  
  29. if (line == null || line.length() < 1) {
  30. continue;
  31. }
  32. String preStic = line.trim().toUpperCase();
  33.  
  34. if (QUIT.equals(preStic) || QUIT_.equals(preStic)) {
  35. break;
  36. }
  37.  
  38. if (HELP.equals(preStic) || HELP_.equals(preStic) || HELP__.equals(preStic)) {
  39. usageInfo();
  40. continue;
  41. }
  42.  
  43. boolean isList = LIST.equals(preStic) || LIST_.equals(preStic) || LISTALL.equals(preStic);
  44. if (isList) {
  45. showAll();
  46. System.out.println("-- listed over --");
  47. }else {
  48. showIndivl(line);
  49. }
  50.  
  51.  
  52. }catch (Exception ex) {
  53.  
  54. usageInfo();
  55. }
  56. }
  57.  
  58. }
  59.  
  60. static void showAll () {
  61. if (props != null) {
  62. props.list(System.out);
  63. }
  64. }
  65.  
  66. static void showIndivl (String key) {
  67. String value = props.getProperty(key, "[NAN]");
  68. StringBuilder info = new StringBuilder(key);
  69. info.append("=").append(value);
  70.  
  71. System.out.println(info);
  72. }
  73.  
  74. public static String get (String key) {
  75. return props.getProperty(key);
  76. }
  77.  
  78. public static Properties set (String key, String value) {
  79. props.setProperty(key, value);
  80. return props;
  81. }
  82.  
  83. static void usageInfo () {
  84. System.out.println("[Usage]: \n\t [/q|/quit]:\t to quit \n\t [/ls|/list|/all]:\t to list all props \n\t {key}:\t get the key-value \n\t [/h|/?|/help]:\t for help");
  85. }
  86. }

URL: http://ideone.com/GqJJJi

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.