source: release-kits/shared/search4j/search4j.cpp@ 15690

Last change on this file since 15690 was 15690, checked in by oranfry, 16 years ago

split search4j into a library, and the utility itself. Yet to be tested for linux.

File size: 6.0 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};
22
23string actionToString( Action a ) {
24 if ( a == Find )
25 return "Find";
26 else if ( a == Launch )
27 return "Launch";
28 else if ( a == Compare )
29 return "Compare";
30 return "Unknown";
31}
32
33string jvmPropertyToString( JvmProperty jp ) {
34 if ( jp == JavaHome )
35 return "JavaHome";
36 else if ( jp == Version )
37 return "Version";
38 else if ( jp == Executable )
39 return "Executable";
40 return "Unknown";
41}
42
43
44int main ( int argc, char** argv ) {
45
46 bool verbose = false;
47 string hint = "";
48 bool use_minimum = false;
49 bool useJavaw = false;
50 Jvm minimum;
51 JvmProperty jvmProperty = JavaHome;
52 Action action = Find;
53 string arg1 = "";
54
55 //parse commandline arguments
56 for (int i=1; i<argc; i++) {
57 if ( strcmp(argv[i], "--verbose") == 0 ) {
58 verbose = true;
59 } else if ( strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 ) {
60 usage();
61 return 0;
62 } else if ( strcmp(argv[i], "-v") == 0 ) {
63 jvmProperty = Version;
64 } else if ( strcmp(argv[i], "-e") == 0 ) {
65 jvmProperty = Executable;
66 } else if ( strcmp(argv[i], "-c") == 0 ) {
67 action = Compare;
68 if ( i == argc-1 ) {
69 usage();
70 return -1;
71 } else {
72 arg1 = argv[++i];
73 }
74
75 } else if ( strcmp(argv[i], "-m") == 0 ) {
76 if ( i == argc-1 ) {
77 usage();
78 return -1;
79 } else {
80 if ( !minimum.setVersionFromString( argv[++i] ) ) {
81 usage();
82 return 0;
83 }
84 use_minimum = true;
85 }
86
87 } else if ( strcmp(argv[i], "-l") == 0 ) {
88 if ( i == argc-1 ) {
89 usage();
90 return -1;
91 } else {
92 action = Launch;
93 arg1 = argv[++i];
94 }
95
96 } else if ( strcmp(argv[i], "-h") == 0 ) {
97 if ( i == argc-1 ) {
98 usage();
99 return -1;
100 } else {
101 hint = argv[++i];
102 }
103
104 #ifdef WINDOWS
105 } else if ( strcmp(argv[i], "-w") == 0 ) {
106 useJavaw = true;
107 #endif
108
109 } else {
110 cout << "unknown option: " << argv[i] << endl;
111 return -1;
112 }
113 }
114
115 //summarise commandline arguments
116 if ( verbose ) {
117 cout << "Action: " << actionToString( action ) << endl;
118 }
119
120 //find java
121 Jvm foundJvm;
122 bool found = find( foundJvm, use_minimum, minimum, hint, verbose );
123
124 //check if it was found
125 if ( found ) {
126 if ( verbose ) cout << "Found JVM at '" << foundJvm.getJavaHome() << "'" << endl;
127 } else {
128 //not found - exit with -1
129 if ( verbose ) cout << "JVM not found" << endl;
130 return -1;
131 }
132
133
134 //Find Action
135 //---------------
136 if ( action == Find ) {
137
138 if ( verbose ) cout << "Property to print: " << jvmPropertyToString( jvmProperty ) << endl;
139
140 //found - print out info about it
141 if ( jvmProperty == JavaHome ) {
142 cout << foundJvm.getJavaHome() << endl;
143 } else if ( jvmProperty == Version ) {
144 cout << foundJvm.getVersion() << endl;
145 } else if ( jvmProperty == Executable ) {
146 if ( useJavaw ) {
147 cout << foundJvm.getWinExecutable() << endl;
148 } else {
149 cout << foundJvm.getExecutable() << endl;
150 }
151 } else {
152 return -1; //should never happen
153 }
154
155
156 //Compare Action
157 //---------------
158 } else if ( action == Compare ) {
159
160 if ( verbose ) cout << "Compare to java at: " << arg1 << endl;
161
162 //load a dummy jvm for comparison
163 Jvm givenJvm;
164 if ( !givenJvm.setVersionFromString(arg1) ) {
165 if ( verbose ) cout << "Could not parse that version string" << endl;
166 return -1;
167 }
168
169 //compare
170 cout << foundJvm.compare( givenJvm );
171 return 0;
172
173
174 //Launch Action
175 //---------------
176 } else if ( action == Launch ) {
177
178 if ( verbose ) cout << "Jar to launch: " << arg1 << endl;
179
180 string cmd = "\"", output = "";
181 if ( useJavaw ) {
182 cmd.append( foundJvm.getWinExecutable() );
183 } else {
184 cmd.append( foundJvm.getExecutable() );
185 }
186 cmd.append( "\" -jar " );
187 cmd.append( arg1 );
188
189 process( cmd, true );
190
191 }
192
193 return 0;
194}
195
196void usage() {
197
198 cout
199 << "-----------------" << endl
200 << " search4j: usage" << endl
201 << "-----------------" << endl
202 << "Three usage methods: find, compare and launch" << endl
203 << endl
204 << "find: find java and print out information about it" << endl
205 << endl
206 << " search4j [-v|-e]" << endl
207 << " eg: search4j -e" << endl
208 << endl
209 << " by default, print JAVA_HOME. E.g., C:\\Program Files\\jre1.5.0_15, or" << endl
210 << " if -v is specified, print the java version string. E.g. 1.5.0_15, or" << endl
211 << " 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
212 << endl
213 << "compare: compare the found java with the given java version string" << endl
214 << endl
215 << " search4j -c VERSION_STRING" << endl
216 << " eg: search4j -c VERSION_STRING" << endl
217 << endl
218 << " print -1 if found java is older" << endl
219 << " print 0 if found java is same" << endl
220 << " print 1 if found java is newer" << endl
221 << endl
222 << "launch: launch the given executable jar with the found java" << endl
223 << endl
224 << " search4j -l EXECUTABLE_JAR [-m VERSION_STRING]" << endl
225 << " eg: search4j -l greenstone3.jar" << endl
226 << endl
227 << " specify the location of the jar relative to the current directory" << endl
228 << endl
229 << "Global Options:" << endl
230 << " -m VERSION_STRING: (minimum) find a java of the given version or newer, or fail" << endl
231 << " -h LOCATION: (hint) as a last resort look for java in LOCATION (treated as a JAVA_HOME)" << endl
232 << " -w: (windows) find and/or use the javaw.exe executable instead of java.exe (in windows only)" << endl
233 << " --verbose : have search4j print out information about what its doing" << endl
234 << " --help : display this usage screen" << endl
235 << endl
236 ;
237}
Note: See TracBrowser for help on using the repository browser.