/Users/lyon/j4p/src/serialPorts/SetPath.java

1    package serialPorts; 
2     
3    import java.util.StringTokenizer; 
4     
5     
6    /** 
7     * Created by 
8     * User: lyon 
9     * Date: Dec 31, 2003 
10    * Time: 9:04:52 AM 
11    * 
12    */ 
13   public class SetPath { 
14       public static final int UNKNOWN = -1; 
15       public static final int MACOSX = 1; 
16       public static final int LINUX = 2; 
17       public static final int WINDOWS = 3; 
18       private static final String libName = "rxtxSerial"; 
19    
20    
21       public static void main(String args[]) { 
22           loadNativeLib(); 
23           System.out.println("done!"); 
24    
25       } 
26       public static String[] getLibPaths() { 
27           String lp = getLibPath(); 
28           String propertySeparator = System.getProperty("path.separator"); 
29           StringTokenizer st = 
30                   new StringTokenizer(lp, propertySeparator); 
31           int n = st.countTokens(); 
32           String s[] = new String[n]; 
33           for (int i = 0; i < n; i++) 
34               s[i] = st.nextToken(); 
35           return s; 
36       } 
37    
38       public static void printNativeLibName() { 
39           String s = getNativeLibName(libName); 
40           System.out.println(s); 
41       } 
42    
43       public static String getNativeLibName(String natLib) { 
44           String s = System.mapLibraryName(natLib); 
45           return s; 
46       } 
47    
48       public static void printPreferredPath() { 
49    
50           switch (getOs()) { 
51               case MACOSX: 
52                   { 
53                       System.out.println("I suggest you put the native lib here:\n" 
54                       +"/Library/Java/Extensions/"+getNativeLibName(libName)); 
55                       break; 
56                   } 
57               case LINUX: 
58                   { 
59                       //System.out.println("Linux not yet supported"); 
60                       break; 
61    
62                   } 
63               case WINDOWS: 
64                   { 
65                       //System.out.println("Windows not yet supported"); 
66                       break; 
67    
68                   } 
69               case UNKNOWN: 
70                   { 
71                       //System.out.println("Unknown OS:"); 
72                       break; 
73    
74                   } 
75           } 
76    
77       } 
78    
79       private static String getLibPath() { 
80           return System.getProperty("java.library.path"); 
81       } 
82    
83       public static void printOs() { 
84           switch (getOs()) { 
85               case MACOSX: 
86                   { 
87                       System.out.println("MACOSX detected"); 
88                       break; 
89                   } 
90               case LINUX: 
91                   { 
92                       System.out.println("Linux detected"); 
93                       break; 
94    
95                   } 
96               case WINDOWS: 
97                   { 
98                       System.out.println("Windows detected"); 
99                       break; 
100   
101                  } 
102              case UNKNOWN: 
103                  { 
104                      System.out.println("Unknown OS"); 
105                      break; 
106   
107                  } 
108          } 
109   
110      } 
111   
112      public static void printLibPath() { 
113          String path[] = getLibPaths(); 
114          for (int i=0; i < path.length; i++) 
115              System.out.println(path[i]); 
116      } 
117   
118      public static int getOs() { 
119          String os = System.getProperty("os.name"); 
120          if (os.equals("Mac OS X")) 
121              return MACOSX; 
122          if (os.equals("Linux")) 
123              return LINUX; 
124          if (os.trim().startsWith("Windows")) 
125              return WINDOWS; 
126          return -1; 
127      } 
128   
129      public static void loadNativeLib() { 
130          try { 
131              System.loadLibrary(libName); 
132          } catch (UnsatisfiedLinkError e) { 
133              reportNativeLibLoadFailed(); 
134   
135          } 
136      } 
137   
138      private static void reportNativeLibLoadFailed() { 
139          String s = "could not load:" + libName 
140          +"\nOn your system, the file:\n"+getNativeLibName(libName) 
141          +" should be loaded into one of the \n" 
142          +"following directories:\n"; 
143   
144          System.out.println(s); 
145          printLibPath(); 
146          printPreferredPath(); 
147      } 
148  } 
149