root/release-kits/shared/search4j/search4j.cpp @ 17302

Revision 17302, 6.5 KB (checked in by oranfry, 11 years ago)

added a priority hint option to search4j

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