source: main/tags/stable/search4j/search4j.cpp@ 24250

Last change on this file since 24250 was 20218, checked in by oranfry, 15 years ago

dont show windows option in linux binary

File size: 7.5 KB
Line 
1/*
2* Search4j utility
3* Command Line utility to search a system for java, launch a jar, or compare java versions
4* base on launch4j
5*/
6
7#include "libsearch4j.h"
8
9void usage();
10
11enum Action {
12 Find,
13 Launch,
14 Compare
15};
16
17enum JvmProperty {
18 JavaHome,
19 Version,
20 Executable,
21 Type
22};
23
24string actionToString( Action a ) {
25 if ( a == Find )
26 return "Find";
27 else if ( a == Launch )
28 return "Launch";
29 else if ( a == Compare )
30 return "Compare";
31 return "Unknown";
32}
33
34string jvmPropertyToString( JvmProperty jp ) {
35 if ( jp == JavaHome )
36 return "JavaHome";
37 else if ( jp == Version )
38 return "Version";
39 else if ( jp == Executable )
40 return "Executable";
41 else if ( jp == Type )
42 return "Type";
43
44 return "Unknown";
45}
46
47
48int main ( int argc, char** argv ) {
49
50 bool verbose = false;
51 string phint = "";
52 string hint = "";
53 bool use_minimum = false;
54 bool useJavaw = false;
55 bool jdkOnly = false;
56 bool jreOnly = false;
57 Jvm minimum;
58 JvmProperty jvmProperty = JavaHome;
59 Action action = Find;
60 string arg1 = "";
61
62 //parse commandline arguments
63 for (int i=1; i<argc; i++) {
64
65 //be verbose
66 if ( strcmp(argv[i], "--verbose") == 0 ) {
67 verbose = true;
68
69 //show help
70 } else if ( strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 ) {
71 usage();
72 return 0;
73
74 //show version
75 } else if ( strcmp(argv[i], "-v") == 0 ) {
76 jvmProperty = Version;
77
78 //show path to executable
79 } else if ( strcmp(argv[i], "-e") == 0 ) {
80 jvmProperty = Executable;
81
82 //show whether jre or jdk
83 } else if ( strcmp(argv[i], "-t") == 0 ) {
84 jvmProperty = Type;
85
86 //compare found java with the given version string
87 } else if ( strcmp(argv[i], "-c") == 0 ) {
88 action = Compare;
89 if ( i == argc-1 ) {
90 usage();
91 return -1;
92 } else {
93 arg1 = argv[++i];
94 }
95
96 //find only javas at or above the given version
97 } else if ( strcmp(argv[i], "-m") == 0 ) {
98 if ( i == argc-1 ) {
99 usage();
100 return -1;
101 } else {
102 if ( !minimum.setVersionFromString( argv[++i] ) ) {
103 usage();
104 return 0;
105 }
106 use_minimum = true;
107 }
108
109 //find only jdks
110 } else if ( strcmp(argv[i], "-d") == 0 ) {
111 jdkOnly = true;
112
113 //find only jres
114 } else if ( strcmp(argv[i], "-r") == 0 ) {
115 jreOnly = true;
116
117 //launch the named jar with the found java
118 } else if ( strcmp(argv[i], "-l") == 0 ) {
119 if ( i == argc-1 ) {
120 usage();
121 return -1;
122 } else {
123 action = Launch;
124 arg1 = argv[++i];
125 }
126
127 //take the given priority hint
128 } else if ( strcmp(argv[i], "-p") == 0 ) {
129 if ( i == argc-1 ) {
130 usage();
131 return -1;
132 } else {
133 phint = argv[++i];
134 }
135
136 // take the following hint
137 } else if ( strcmp(argv[i], "-h") == 0 ) {
138 if ( i == argc-1 ) {
139 usage();
140 return -1;
141 } else {
142 hint = argv[++i];
143 }
144
145 #ifdef WINDOWS
146 //show the path to the javaw executable
147 } else if ( strcmp(argv[i], "-w") == 0 ) {
148 useJavaw = true;
149 #endif
150
151 } else {
152 cout << "unknown option: " << argv[i] << endl;
153 return -1;
154 }
155 }
156
157 //summarise commandline arguments
158 if ( verbose ) {
159 cout << "Action: " << actionToString( action ) << endl;
160 }
161
162 //find java
163 Jvm foundJvm;
164 bool found = find( foundJvm, use_minimum, minimum, jreOnly, jdkOnly, phint, hint, verbose );
165
166 //check if it was found
167 if ( found ) {
168 if ( verbose ) cout << "Found JVM at '" << foundJvm.getJavaHome() << "'" << endl;
169 } else {
170 //not found - exit with -1
171 if ( verbose ) cout << "JVM not found" << endl;
172 return -1;
173 }
174
175
176 //Find Action
177 //---------------
178 if ( action == Find ) {
179
180 if ( verbose ) cout << "Property to print: " << jvmPropertyToString( jvmProperty ) << endl;
181
182 //found - print out info about it
183 if ( jvmProperty == JavaHome ) {
184 cout << foundJvm.getJavaHome() << endl;
185 } else if ( jvmProperty == Version ) {
186 cout << foundJvm.getVersion() << endl;
187 } else if ( jvmProperty == Executable ) {
188 #ifdef WINDOWS
189 if ( useJavaw ) {
190 cout << foundJvm.getWinExecutable() << endl;
191 } else {
192 cout << foundJvm.getExecutable() << endl;
193 }
194 #else
195 cout << foundJvm.getExecutable() << endl;
196 #endif
197 } else if ( jvmProperty == Type ) {
198 if ( foundJvm.getIsJdk() ) {
199 cout << "JDK" << endl;
200 } else {
201 cout << "JRE" << endl;
202 }
203 } else {
204 return -1; //should never happen
205 }
206
207
208 //Compare Action
209 //---------------
210 } else if ( action == Compare ) {
211
212 if ( verbose ) cout << "Compare to java at: " << arg1 << endl;
213
214 //load a dummy jvm for comparison
215 Jvm givenJvm;
216 if ( !givenJvm.setVersionFromString(arg1) ) {
217 if ( verbose ) cout << "Could not parse that version string" << endl;
218 return -1;
219 }
220
221 //compare
222 cout << foundJvm.compare( givenJvm );
223 return 0;
224
225
226 //Launch Action
227 //---------------
228 } else if ( action == Launch ) {
229
230 if ( verbose ) cout << "Jar to launch: " << arg1 << endl;
231
232 string cmd = "\"", output = "";
233 #ifdef WINDOWS
234 if ( useJavaw ) {
235 cmd.append( foundJvm.getWinExecutable() );
236 } else {
237 cmd.append( foundJvm.getExecutable() );
238 }
239 #else
240 cmd.append( foundJvm.getExecutable() );
241 #endif
242 cmd.append( "\" -jar " );
243 cmd.append( arg1 );
244
245 process( cmd, true );
246
247 }
248
249 return 0;
250}
251
252void usage() {
253
254 cout
255 << "-----------------" << endl
256 << " search4j: usage" << endl
257 << "-----------------" << endl
258 << "Three usage methods: find, compare and launch" << endl
259 << endl
260 << "find: find java and print out information about it" << endl
261 << endl
262 << " search4j [-v|-e|-t]" << endl
263 << " eg: search4j -e" << endl
264 << endl
265 << " by default, print JAVA_HOME. E.g., C:\\Program Files\\jre1.5.0_15, or" << endl
266 << " if -v is specified, print the java version string. E.g. 1.5.0_15, or" << endl
267 << " if -e is specified, print the path the to the java executable. E.g. C:\\Program Files\\jre1.5.0_15\\bin\\java.exe" << endl
268 << " if -t is specified, print whether the found java is a JRE or a JDK" << endl
269 << endl
270 << "compare: compare the found java with the given java version string" << endl
271 << endl
272 << " search4j -c VERSION_STRING" << endl
273 << " eg: search4j -c VERSION_STRING" << endl
274 << endl
275 << " print -1 if found java is older" << endl
276 << " print 0 if found java is same" << endl
277 << " print 1 if found java is newer" << endl
278 << endl
279 << "launch: launch the given executable jar with the found java" << endl
280 << endl
281 << " search4j -l EXECUTABLE_JAR [-m VERSION_STRING]" << endl
282 << " eg: search4j -l greenstone3.jar" << endl
283 << endl
284 << " specify the location of the jar relative to the current directory" << endl
285 << endl
286 << "Global Options:" << endl
287 << " -m VERSION_STRING: (minimum) find a java of the given version or newer, or fail" << endl
288 << " -d : find only jdks" << endl
289 << " -r : find only jres" << endl
290 << " -p LOCATION: (priority hint) first look for java in LOCATION (treated as a JAVA_HOME)" << endl
291 << " -h LOCATION: (hint) as a last resort look for java in LOCATION (treated as a JAVA_HOME)" << endl
292 #ifdef WINDOWS
293 << " -w: (windows) find and/or use the javaw.exe executable instead of java.exe (in windows only)" << endl
294 #endif
295 << " --verbose : have search4j print out information about what its doing" << endl
296 << " --help : display this usage screen" << endl
297 << endl
298 ;
299}
Note: See TracBrowser for help on using the repository browser.