[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 |
|
---|
| 7 | #include <iostream>
|
---|
| 8 | #include <string>
|
---|
| 9 | #include <windows.h>
|
---|
| 10 | using namespace std;
|
---|
| 11 |
|
---|
| 12 | bool verbose = false;
|
---|
| 13 |
|
---|
| 14 | enum JvmProperty {
|
---|
| 15 | JavaHome,
|
---|
| 16 | Version,
|
---|
| 17 | Executable
|
---|
| 18 | };
|
---|
| 19 |
|
---|
| 20 | enum Action {
|
---|
| 21 | Find,
|
---|
| 22 | Launch,
|
---|
| 23 | Compare
|
---|
| 24 | };
|
---|
| 25 |
|
---|
| 26 | int process( char* command, string &output ) {
|
---|
| 27 | PROCESS_INFORMATION pi;
|
---|
| 28 | STARTUPINFO si;
|
---|
| 29 | memset(&pi, 0, sizeof(pi));
|
---|
| 30 | memset(&si, 0, sizeof(si));
|
---|
| 31 | si.cb = sizeof(si);
|
---|
| 32 |
|
---|
| 33 | DWORD dwExitCode = -1;
|
---|
| 34 | char cmdline[1024];
|
---|
| 35 | strcpy(cmdline, "dir");
|
---|
| 36 |
|
---|
| 37 | if ( CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) ) {
|
---|
| 38 | WaitForSingleObject(pi.hProcess, INFINITE);
|
---|
| 39 | GetExitCodeProcess(pi.hProcess, &dwExitCode);
|
---|
| 40 | CloseHandle(pi.hThread);
|
---|
| 41 | CloseHandle(pi.hProcess);
|
---|
| 42 | }
|
---|
| 43 | return 1;
|
---|
| 44 |
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | class Jvm {
|
---|
| 48 |
|
---|
| 49 | string javaHome;
|
---|
| 50 | bool isJdk;
|
---|
| 51 |
|
---|
| 52 | // version string. era.major.minor_update. eg: 1.4.2_02
|
---|
| 53 | string version;
|
---|
| 54 | int era;
|
---|
| 55 | int major;
|
---|
| 56 | int minor;
|
---|
| 57 | int update;
|
---|
| 58 |
|
---|
| 59 | public:
|
---|
| 60 |
|
---|
| 61 | void setJavaHome( string jh ) {
|
---|
| 62 | javaHome = jh;
|
---|
| 63 | setVersion();
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 | string getJavaHome() {
|
---|
| 67 | return javaHome;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | string getExecutable() {
|
---|
| 71 | string exec = "";
|
---|
| 72 | exec.append( javaHome );
|
---|
| 73 | exec.append( "\\bin\\java.exe" );
|
---|
| 74 | return exec;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | string getVersion() {
|
---|
| 78 | return version;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | bool check() {
|
---|
| 82 | return true;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | private:
|
---|
| 86 |
|
---|
| 87 | void setVersion() {
|
---|
| 88 |
|
---|
| 89 | string command = "", output="";
|
---|
| 90 | command.append( getExecutable() );
|
---|
| 91 | command.append(" -version");
|
---|
| 92 | //bool result = process( command, output );
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | };
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | string actionToString( Action a ) {
|
---|
| 100 | if ( a == Find )
|
---|
| 101 | return "Find";
|
---|
| 102 | else if ( a == Launch )
|
---|
| 103 | return "Launch";
|
---|
| 104 | else if ( a == Compare )
|
---|
| 105 | return "Compare";
|
---|
| 106 | return "Unknown";
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | string jvmPropertyToString( JvmProperty jp ) {
|
---|
| 110 | if ( jp == JavaHome )
|
---|
| 111 | return "JavaHome";
|
---|
| 112 | else if ( jp == Version )
|
---|
| 113 | return "Version";
|
---|
| 114 | else if ( jp == Executable )
|
---|
| 115 | return "Executable";
|
---|
| 116 | return "Unknown";
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | void usage() {
|
---|
| 120 |
|
---|
| 121 | cout
|
---|
| 122 | << "-----------------" << endl
|
---|
| 123 | << " search4j: usage" << endl
|
---|
| 124 | << "-----------------" << endl
|
---|
| 125 | << "Three usage methods: find, compare and launch" << endl
|
---|
| 126 | << endl
|
---|
| 127 | << "find: find java and print out information about it" << endl
|
---|
| 128 | << endl
|
---|
| 129 | << " search4j [-v|-e] [-m VERSION_STRING]" << endl
|
---|
| 130 | << " eg: search4j -m 1.4.1_01 -e" << endl
|
---|
| 131 | << endl
|
---|
| 132 | << " by default, print JAVA_HOME. E.g., C:\\Program Files\\jre1.5.0_15, or" << endl
|
---|
| 133 | << " if -v is specified, print the java version string. E.g. 1.5.0_15, or" << endl
|
---|
| 134 | << " 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
|
---|
| 135 | << " if -m option is used, find a java of the given version or newer, or fail" << endl
|
---|
| 136 | << endl
|
---|
| 137 | << "compare: compare the found java with the given java version string" << endl
|
---|
| 138 | << endl
|
---|
| 139 | << " search4j -c VERSION_STRING" << endl
|
---|
| 140 | << " eg: search4j -c VERSION_STRING" << endl
|
---|
| 141 | << endl
|
---|
| 142 | << " print -1 if found java is older" << endl
|
---|
| 143 | << " print 0 if found java is same" << endl
|
---|
| 144 | << " print 1 if found java is newer" << endl
|
---|
| 145 | << endl
|
---|
| 146 | << "launch: launch the given executable jar with the found java" << endl
|
---|
| 147 | << endl
|
---|
| 148 | << " search4j -l EXECUTABLE_JAR [-m VERSION_STRING]" << endl
|
---|
| 149 | << " eg: search4j -l greenstone3.jar" << endl
|
---|
| 150 | << endl
|
---|
| 151 | << " specify the location of the jar relative to the current directory" << endl
|
---|
| 152 | << " if -m option is used, find a java of the given version or newer" << endl
|
---|
| 153 | << endl
|
---|
| 154 | << "Global Options:" << endl
|
---|
| 155 | << " --verbose : have search4j print out information about what its doing" << endl
|
---|
| 156 | << " --help : display this usage screen" << endl
|
---|
| 157 | << endl
|
---|
| 158 | ;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | /*
|
---|
| 163 | * function to find java
|
---|
| 164 | * implements the logic drawn on the dl lab whiteboard in feb 08
|
---|
| 165 | * return a Jvm object which represents the jvm on disk
|
---|
| 166 | */
|
---|
| 167 | bool find( Jvm &jvm ) {
|
---|
| 168 |
|
---|
| 169 | if ( verbose ) cout << "Searching for a JVM" << endl;
|
---|
| 170 | char *javaHome = "";
|
---|
| 171 | bool jvmFound = false;
|
---|
| 172 |
|
---|
| 173 | if ( !jvmFound ) {
|
---|
| 174 | //try JAVA_HOME
|
---|
| 175 | if ( verbose ) cout << " - trying JAVA_HOME" << endl;
|
---|
| 176 | javaHome = getenv( "JAVA_HOME" );
|
---|
| 177 | if ( javaHome != NULL ) {
|
---|
| 178 | jvm.setJavaHome( javaHome );
|
---|
| 179 | if ( jvm.check() ) {
|
---|
| 180 | jvmFound = true;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | if ( !jvmFound ) {
|
---|
| 186 | //try JRE_HOME
|
---|
| 187 | if ( verbose ) cout << " - trying JRE_HOME" << endl;
|
---|
| 188 | javaHome = getenv( "JRE_HOME" );
|
---|
| 189 | if ( javaHome != NULL ) {
|
---|
| 190 | jvm.setJavaHome( javaHome );
|
---|
| 191 | if ( jvm.check() ) {
|
---|
| 192 | jvmFound = true;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | return jvmFound;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 |
|
---|
| 201 | int main ( int argc, char** argv ) {
|
---|
| 202 |
|
---|
| 203 | string output = "";
|
---|
| 204 | int result = process("dir", output);
|
---|
| 205 | cout << "dir:" << endl << output << endl << result << endl;
|
---|
| 206 |
|
---|
| 207 | JvmProperty jvmProperty = JavaHome;
|
---|
| 208 | Action action = Find;
|
---|
| 209 | string arg1 = "";
|
---|
| 210 |
|
---|
| 211 | //parse commandline arguments
|
---|
| 212 | for (int i=1; i<argc; i++) {
|
---|
| 213 | if ( _stricmp(argv[i], "--verbose") == 0 ) {
|
---|
| 214 | verbose = true;
|
---|
| 215 | } else if ( _stricmp(argv[i], "--help") == 0 || _stricmp(argv[i], "--usage") == 0 ) {
|
---|
| 216 | usage();
|
---|
| 217 | return 0;
|
---|
| 218 | } else if ( _stricmp(argv[i], "-v") == 0 ) {
|
---|
| 219 | jvmProperty = Version;
|
---|
| 220 | } else if ( _stricmp(argv[i], "-e") == 0 ) {
|
---|
| 221 | jvmProperty = Executable;
|
---|
| 222 | } else if ( _stricmp(argv[i], "-c") == 0 ) {
|
---|
| 223 | action = Compare;
|
---|
| 224 | if ( i == argc-1 ) {
|
---|
| 225 | usage();
|
---|
| 226 | return 0;
|
---|
| 227 | } else {
|
---|
| 228 | arg1 = argv[++i];
|
---|
| 229 | }
|
---|
| 230 | } else if ( _stricmp(argv[i], "-l") == 0 ) {
|
---|
| 231 | action = Launch;
|
---|
| 232 | arg1 = argv[++i];
|
---|
| 233 | } else {
|
---|
| 234 | cout << "unknown option: " << argv[i] << endl;
|
---|
| 235 | return -1;
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | //summarise commandline arguments
|
---|
| 240 | if ( verbose ) {
|
---|
| 241 | cout << "Action: " << actionToString( action ) << endl;
|
---|
| 242 | if ( action == Find ) cout << "Property to print: " << jvmPropertyToString( jvmProperty ) << endl;
|
---|
| 243 | if ( action == Compare ) cout << "Compare to java at: " << arg1 << endl;
|
---|
| 244 | if ( action == Launch ) cout << "Jar to launch: " << arg1 << endl;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | //Find Action
|
---|
| 249 | //---------------
|
---|
| 250 | if ( action == Find ) {
|
---|
| 251 |
|
---|
| 252 | //find java
|
---|
| 253 | Jvm jvm;
|
---|
| 254 | bool found = find( jvm );
|
---|
| 255 |
|
---|
| 256 | //check if it was found
|
---|
| 257 | if ( found ) {
|
---|
| 258 |
|
---|
| 259 | //found - print out info about it
|
---|
| 260 | if ( verbose ) cout << "JVM found!" << endl;
|
---|
| 261 | if ( jvmProperty == JavaHome ) {
|
---|
| 262 | cout << jvm.getJavaHome() << endl;
|
---|
| 263 | } else if ( jvmProperty == Version ) {
|
---|
| 264 | cout << jvm.getVersion() << endl;
|
---|
| 265 | } else if ( jvmProperty == Version ) {
|
---|
| 266 | cout << jvm.getExecutable() << endl;
|
---|
| 267 | } else {
|
---|
| 268 | return -1; //should never happen
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | } else {
|
---|
| 272 |
|
---|
| 273 | //not found - exit with -1
|
---|
| 274 | if ( verbose ) cout << "JVM not found" << endl;
|
---|
| 275 | return -1;
|
---|
| 276 |
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 |
|
---|
| 281 | //Compare Action
|
---|
| 282 | //---------------
|
---|
| 283 | } else if ( action == Compare ) {
|
---|
| 284 | //compare();
|
---|
| 285 |
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 | //Launch Action
|
---|
| 289 | //---------------
|
---|
| 290 | } else if ( action == Launch ) {
|
---|
| 291 | //launch();
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 |
|
---|
| 295 | return 0;
|
---|
| 296 | } |
---|