[15093] | 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 |
|
---|
[15690] | 7 | #include "libsearch4j.h"
|
---|
[15093] | 8 |
|
---|
[15690] | 9 | void usage();
|
---|
[15093] | 10 |
|
---|
| 11 | enum Action {
|
---|
| 12 | Find,
|
---|
| 13 | Launch,
|
---|
| 14 | Compare
|
---|
| 15 | };
|
---|
| 16 |
|
---|
[15690] | 17 | enum JvmProperty {
|
---|
| 18 | JavaHome,
|
---|
| 19 | Version,
|
---|
[18237] | 20 | Executable,
|
---|
| 21 | Type
|
---|
[15093] | 22 | };
|
---|
| 23 |
|
---|
| 24 | string actionToString( Action a ) {
|
---|
| 25 | if ( a == Find )
|
---|
| 26 | return "Find";
|
---|
| 27 | else if ( a == Launch )
|
---|
| 28 | return "Launch";
|
---|
| 29 | else if ( a == Compare )
|
---|
[18237] | 30 | return "Compare";
|
---|
[15093] | 31 | return "Unknown";
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | string 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";
|
---|
[18237] | 41 | else if ( jp == Type )
|
---|
| 42 | return "Type";
|
---|
| 43 |
|
---|
[15093] | 44 | return "Unknown";
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | int main ( int argc, char** argv ) {
|
---|
| 49 |
|
---|
[15690] | 50 | bool verbose = false;
|
---|
[17302] | 51 | string phint = "";
|
---|
[15690] | 52 | string hint = "";
|
---|
| 53 | bool use_minimum = false;
|
---|
[18237] | 54 | bool useJavaw = false;
|
---|
| 55 | bool jdkOnly = false;
|
---|
| 56 | bool jreOnly = false;
|
---|
| 57 | Jvm minimum;
|
---|
[15093] | 58 | JvmProperty jvmProperty = JavaHome;
|
---|
| 59 | Action action = Find;
|
---|
| 60 | string arg1 = "";
|
---|
| 61 |
|
---|
| 62 | //parse commandline arguments
|
---|
| 63 | for (int i=1; i<argc; i++) {
|
---|
[18237] | 64 |
|
---|
| 65 | //be verbose
|
---|
[15103] | 66 | if ( strcmp(argv[i], "--verbose") == 0 ) {
|
---|
[15093] | 67 | verbose = true;
|
---|
[18237] | 68 |
|
---|
| 69 | //show help
|
---|
[15103] | 70 | } else if ( strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "--usage") == 0 ) {
|
---|
[15093] | 71 | usage();
|
---|
| 72 | return 0;
|
---|
[18237] | 73 |
|
---|
| 74 | //show version
|
---|
[15103] | 75 | } else if ( strcmp(argv[i], "-v") == 0 ) {
|
---|
[15093] | 76 | jvmProperty = Version;
|
---|
[18237] | 77 |
|
---|
| 78 | //show path to executable
|
---|
[15103] | 79 | } else if ( strcmp(argv[i], "-e") == 0 ) {
|
---|
[15093] | 80 | jvmProperty = Executable;
|
---|
[18237] | 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
|
---|
[15103] | 87 | } else if ( strcmp(argv[i], "-c") == 0 ) {
|
---|
[15093] | 88 | action = Compare;
|
---|
| 89 | if ( i == argc-1 ) {
|
---|
| 90 | usage();
|
---|
[15133] | 91 | return -1;
|
---|
[15093] | 92 | } else {
|
---|
| 93 | arg1 = argv[++i];
|
---|
| 94 | }
|
---|
[18237] | 95 |
|
---|
| 96 | //find only javas at or above the given version
|
---|
[15133] | 97 | } else if ( strcmp(argv[i], "-m") == 0 ) {
|
---|
[15093] | 98 | if ( i == argc-1 ) {
|
---|
| 99 | usage();
|
---|
[15133] | 100 | return -1;
|
---|
[15093] | 101 | } else {
|
---|
| 102 | if ( !minimum.setVersionFromString( argv[++i] ) ) {
|
---|
| 103 | usage();
|
---|
| 104 | return 0;
|
---|
| 105 | }
|
---|
| 106 | use_minimum = true;
|
---|
[18237] | 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
|
---|
[15103] | 118 | } else if ( strcmp(argv[i], "-l") == 0 ) {
|
---|
[15133] | 119 | if ( i == argc-1 ) {
|
---|
| 120 | usage();
|
---|
| 121 | return -1;
|
---|
| 122 | } else {
|
---|
| 123 | action = Launch;
|
---|
[15690] | 124 | arg1 = argv[++i];
|
---|
[15133] | 125 | }
|
---|
[18237] | 126 |
|
---|
| 127 | //take the given priority hint
|
---|
[17302] | 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 | }
|
---|
[18237] | 135 |
|
---|
| 136 | // take the following hint
|
---|
[15690] | 137 | } else if ( strcmp(argv[i], "-h") == 0 ) {
|
---|
[15133] | 138 | if ( i == argc-1 ) {
|
---|
| 139 | usage();
|
---|
| 140 | return -1;
|
---|
| 141 | } else {
|
---|
[15690] | 142 | hint = argv[++i];
|
---|
| 143 | }
|
---|
[18237] | 144 |
|
---|
[15690] | 145 | #ifdef WINDOWS
|
---|
[18237] | 146 | //show the path to the javaw executable
|
---|
[15690] | 147 | } else if ( strcmp(argv[i], "-w") == 0 ) {
|
---|
| 148 | useJavaw = true;
|
---|
| 149 | #endif
|
---|
[15093] | 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;
|
---|
[18237] | 164 | bool found = find( foundJvm, use_minimum, minimum, jreOnly, jdkOnly, phint, hint, verbose );
|
---|
[15093] | 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 |
|
---|
[18237] | 180 | if ( verbose ) cout << "Property to print: " << jvmPropertyToString( jvmProperty ) << endl;
|
---|
[15093] | 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 ) {
|
---|
[15892] | 188 | #ifdef WINDOWS
|
---|
[15690] | 189 | if ( useJavaw ) {
|
---|
| 190 | cout << foundJvm.getWinExecutable() << endl;
|
---|
| 191 | } else {
|
---|
| 192 | cout << foundJvm.getExecutable() << endl;
|
---|
[15892] | 193 | }
|
---|
| 194 | #else
|
---|
| 195 | cout << foundJvm.getExecutable() << endl;
|
---|
| 196 | #endif
|
---|
[18237] | 197 | } else if ( jvmProperty == Type ) {
|
---|
| 198 | if ( foundJvm.getIsJdk() ) {
|
---|
| 199 | cout << "JDK" << endl;
|
---|
| 200 | } else {
|
---|
| 201 | cout << "JRE" << endl;
|
---|
| 202 | }
|
---|
[15093] | 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 = "";
|
---|
[15892] | 233 | #ifdef WINDOWS
|
---|
[15690] | 234 | if ( useJavaw ) {
|
---|
| 235 | cmd.append( foundJvm.getWinExecutable() );
|
---|
| 236 | } else {
|
---|
| 237 | cmd.append( foundJvm.getExecutable() );
|
---|
[15892] | 238 | }
|
---|
| 239 | #else
|
---|
| 240 | cmd.append( foundJvm.getExecutable() );
|
---|
| 241 | #endif
|
---|
[15093] | 242 | cmd.append( "\" -jar " );
|
---|
| 243 | cmd.append( arg1 );
|
---|
| 244 |
|
---|
[15690] | 245 | process( cmd, true );
|
---|
[15093] | 246 |
|
---|
| 247 | }
|
---|
| 248 |
|
---|
| 249 | return 0;
|
---|
[15690] | 250 | }
|
---|
| 251 |
|
---|
| 252 | void 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
|
---|
[18237] | 262 | << " search4j [-v|-e|-t]" << endl
|
---|
[15690] | 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
|
---|
[18237] | 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
|
---|
[15690] | 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
|
---|
[18237] | 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
|
---|
[17302] | 290 | << " -p LOCATION: (priority hint) first look for java in LOCATION (treated as a JAVA_HOME)" << endl
|
---|
[15690] | 291 | << " -h LOCATION: (hint) as a last resort look for java in LOCATION (treated as a JAVA_HOME)" << endl
|
---|
| 292 | << " -w: (windows) find and/or use the javaw.exe executable instead of java.exe (in windows only)" << endl
|
---|
| 293 | << " --verbose : have search4j print out information about what its doing" << endl
|
---|
| 294 | << " --help : display this usage screen" << endl
|
---|
| 295 | << endl
|
---|
| 296 | ;
|
---|
| 297 | }
|
---|