/Users/lyon/j4p/src/utils/SystemUtils.java

1    package utils; 
2     
3    import futils.Futil; 
4     
5    import java.io.File; 
6    import java.util.Enumeration; 
7    import java.util.Properties; 
8     
9    /** 
10    * DocJava, Inc. http://www.docjava.com Programmer: dlyon Date: Mar 29, 
11    * 2004 Time: 3:33:32 PM 
12    */ 
13   public class SystemUtils { 
14    
15       public static boolean isWindows() { 
16           String osName = getOsName(); 
17           return osName.startsWith("Windows"); 
18       } 
19    
20       public static String getOsName() { 
21           Properties prop = System.getProperties(); 
22           return prop.getProperty("os.name"); 
23       } 
24    
25       /** 
26        * This works on windows and one the mac. 
27        * It has not been tested on all platforms! 
28        * 
29        * @return location of the rmic compiler. 
30        */ 
31       public static File getRmicPath() { 
32           // first try the windows location 
33           String sh = getSdkHome(); 
34           System.out.println("sdkhome="+getSdkHome()); 
35           String fs = getDirectorySeparator(); 
36           sh = sh + 
37                   fs + 
38                   "bin" + 
39                   fs + "rmic.exe"; 
40           File f = new File(sh); 
41           if (f.exists()) return f; 
42           // ok, that did not work..hmm 
43           // lets try the mac location. 
44           f = new File(fs + "usr" + fs + "bin" + fs + "rmic"); 
45           if (f.exists()) return f; 
46           //  OK, now I am stuck, lets ask the user: 
47           f = Futil.getReadFile("please locate rmic"); 
48           System.out.println( 
49                   "please add:" + f + "to the getRmicPath in SystemUtils"); 
50           return f; 
51       } 
52    
53       public static String getSdkHome() { 
54           String jh = getJavaHome(); 
55           File f = new File(jh); 
56           return f.getParent(); 
57       } 
58    
59       public static String getJavaHome() { 
60           Properties prop = System.getProperties(); 
61           return prop.getProperty("java.home"); 
62       } 
63    
64       /** 
65        * @return the location of the program that is running. Typically 
66        *         something like:<br><code> /User/lyon/current/j4p</code>. 
67        */ 
68       public static String getUserDir() { 
69           return System.getProperty("user.dir"); 
70       } 
71    
72       /** 
73        * @return the character(s) used to delimit directories in path names. 
74        *         For example:<br> /home/lyon/foo, the file separator is "/". 
75        */ 
76       public static String getDirectorySeparator() { 
77           return System.getProperty("file.separator"); 
78       } 
79    
80       /** 
81        * @return return the character use to delimit the paths in a list of 
82        *         paths. 
83        */ 
84       public static String getPathSeparator() { 
85           return System.getProperty("path.separator"); 
86       } 
87    
88       public static void printProps() { 
89           Properties prop = System.getProperties(); 
90           prop.list(System.out); 
91       } 
92    
93       public static void main(String[] args) { 
94           printProps(); 
95           System.out.println("------------------------"); 
96           System.out.println("os.name=" + getOsName()); 
97           System.out.println("isWindows=" + isWindows()); 
98           System.out.println("free ram=" + free()); 
99           System.out.println("userDir=" + getUserDir()); 
100          System.out.println("userHome="+getUserHome()); 
101          System.out.println("path sep = " + getPathSeparator()); 
102          System.out.println("file sep = " + getDirectorySeparator()); 
103          System.out.println("getSdkHome=" + getSdkHome()); 
104          System.out.println("getRmicPath=" + getRmicPath()); 
105          System.out.println("getClassPath=" + SystemUtils.getClassPath()); 
106   
107      } 
108   
109      /** 
110       * @return a string that describes the JVM's free RAM 
111       */ 
112      public static String free() { 
113          futils.Exec e = new futils.Exec(); 
114          return e.getPrintRamString(); 
115      } 
116   
117      public static void props() { 
118          Properties props = System.getProperties(); 
119          for (Enumeration e = 
120                  props.propertyNames(); e.hasMoreElements();) { 
121              String key = (String) e.nextElement(); 
122              Print.println(key + " = " + (String) (props.get(key))); 
123          } 
124      } 
125   
126      public static void printProp(String p) { 
127          System.out.println(p + ":" + System.getProperty(p)); 
128      } 
129   
130      public static String getClassPath() { 
131          return System.getProperty("java.class.path"); 
132      } 
133   
134      public static void home() { 
135          printProp("java.home"); 
136      } 
137   
138      public static void userDir() { 
139          printProp("user.dir"); 
140      } 
141     /** 
142      * Returns the users home directory. For example: 
143      * <code>C:\Documents and Settings\dlyon</code> 
144      * 
145      */ 
146      public static String getUserHome() { 
147         return System.getProperty("user.home"); 
148     } 
149  } 
150   
151  /* 
152  java.runtime.name=Java(TM) 2 Runtime Environment, Stand... 
153  sun.boot.library.path=C:\j2sdk1.4.1_02\jre\bin 
154  java.vm.version=1.4.1_02-b06 
155  java.vm.vendor=Sun Microsystems Inc. 
156  java.vendor.url=http://java.sun.com/ 
157  path.separator=; 
158  java.vm.name=Java HotSpot(TM) Client VM 
159  file.encoding.pkg=sun.io 
160  idea.launcher.port=7556 
161  user.country=US 
162  sun.os.patch.level=Service Pack 1 
163  java.vm.specification.name=Java Virtual Machine Specification 
164  user.dir=C:\lyon\j4p 
165  java.runtime.version=1.4.1_02-b06 
166  java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment 
167  java.endorsed.dirs=C:\j2sdk1.4.1_02\jre\lib\endorsed 
168  os.arch=x86 
169  java.io.tmpdir=C:\DOCUME~1\dlyon\LOCALS~1\Temp\ 
170  line.separator= 
171   
172  java.vm.specification.vendor=Sun Microsystems Inc. 
173  user.variant= 
174  os.name=Windows XP 
175  sun.java2d.fontpath= 
176  java.library.path=C:\j2sdk1.4.1_02\bin;.;C:\WINDOWS\Sys... 
177  java.specification.name=Java Platform API Specification 
178  java.class.version=48.0 
179  java.util.prefs.PreferencesFactory=java.util.prefs.WindowsPreferencesFac... 
180  os.version=5.1 
181  user.home=C:\Documents and Settings\dlyon 
182  user.timezone= 
183  java.awt.printerjob=sun.awt.windows.WPrinterJob 
184  file.encoding=windows-1252 
185  java.specification.version=1.4 
186  user.name=dlyon 
187  java.class.path=C:\j2sdk1.4.1_02\jre\lib\charsets.jar... 
188  java.vm.specification.version=1.0 
189  sun.arch.data.model=32 
190  java.home=C:\j2sdk1.4.1_02\jre 
191  java.specification.vendor=Sun Microsystems Inc. 
192  user.language=en 
193  awt.toolkit=sun.awt.windows.WToolkit 
194  java.vm.info=mixed mode 
195  java.version=1.4.1_02 
196  java.ext.dirs=C:\j2sdk1.4.1_02\jre\lib\ext 
197  sun.boot.class.path=C:\j2sdk1.4.1_02\jre\lib\rt.jar;C:\j2... 
198  java.vendor=Sun Microsystems Inc. 
199  file.separator=\ 
200  java.vendor.url.bug=http://java.sun.com/cgi-bin/bugreport... 
201  idea.launcher.library=C:\IntelliJ-IDEA-4.0\bin\breakgen.dll 
202  sun.cpu.endian=little 
203  sun.io.unicode.encoding=UnicodeLittle 
204  sun.cpu.isalist=pentium i486 i386 
205  */ 
206