/* * Search4j utility * Command Line utility to search a system for java, launch a jar, or compare java versions * base on launch4j */ #include #include #include using namespace std; bool verbose = false; enum JvmProperty { JavaHome, Version, Executable }; enum Action { Find, Launch, Compare }; int process( char* command, string &output ) { PROCESS_INFORMATION pi; STARTUPINFO si; memset(&pi, 0, sizeof(pi)); memset(&si, 0, sizeof(si)); si.cb = sizeof(si); DWORD dwExitCode = -1; char cmdline[1024]; strcpy(cmdline, "dir"); if ( CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) ) { WaitForSingleObject(pi.hProcess, INFINITE); GetExitCodeProcess(pi.hProcess, &dwExitCode); CloseHandle(pi.hThread); CloseHandle(pi.hProcess); } return 1; } class Jvm { string javaHome; bool isJdk; // version string. era.major.minor_update. eg: 1.4.2_02 string version; int era; int major; int minor; int update; public: void setJavaHome( string jh ) { javaHome = jh; setVersion(); } string getJavaHome() { return javaHome; } string getExecutable() { string exec = ""; exec.append( javaHome ); exec.append( "\\bin\\java.exe" ); return exec; } string getVersion() { return version; } bool check() { return true; } private: void setVersion() { string command = "", output=""; command.append( getExecutable() ); command.append(" -version"); //bool result = process( command, output ); } }; string actionToString( Action a ) { if ( a == Find ) return "Find"; else if ( a == Launch ) return "Launch"; else if ( a == Compare ) return "Compare"; return "Unknown"; } string jvmPropertyToString( JvmProperty jp ) { if ( jp == JavaHome ) return "JavaHome"; else if ( jp == Version ) return "Version"; else if ( jp == Executable ) return "Executable"; return "Unknown"; } void usage() { cout << "-----------------" << endl << " search4j: usage" << endl << "-----------------" << endl << "Three usage methods: find, compare and launch" << endl << endl << "find: find java and print out information about it" << endl << endl << " search4j [-v|-e] [-m VERSION_STRING]" << endl << " eg: search4j -m 1.4.1_01 -e" << endl << endl << " by default, print JAVA_HOME. E.g., C:\\Program Files\\jre1.5.0_15, or" << endl << " if -v is specified, print the java version string. E.g. 1.5.0_15, or" << endl << " 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 << " if -m option is used, find a java of the given version or newer, or fail" << endl << endl << "compare: compare the found java with the given java version string" << endl << endl << " search4j -c VERSION_STRING" << endl << " eg: search4j -c VERSION_STRING" << endl << endl << " print -1 if found java is older" << endl << " print 0 if found java is same" << endl << " print 1 if found java is newer" << endl << endl << "launch: launch the given executable jar with the found java" << endl << endl << " search4j -l EXECUTABLE_JAR [-m VERSION_STRING]" << endl << " eg: search4j -l greenstone3.jar" << endl << endl << " specify the location of the jar relative to the current directory" << endl << " if -m option is used, find a java of the given version or newer" << endl << endl << "Global Options:" << endl << " --verbose : have search4j print out information about what its doing" << endl << " --help : display this usage screen" << endl << endl ; } /* * function to find java * implements the logic drawn on the dl lab whiteboard in feb 08 * return a Jvm object which represents the jvm on disk */ bool find( Jvm &jvm ) { if ( verbose ) cout << "Searching for a JVM" << endl; char *javaHome = ""; bool jvmFound = false; if ( !jvmFound ) { //try JAVA_HOME if ( verbose ) cout << " - trying JAVA_HOME" << endl; javaHome = getenv( "JAVA_HOME" ); if ( javaHome != NULL ) { jvm.setJavaHome( javaHome ); if ( jvm.check() ) { jvmFound = true; } } } if ( !jvmFound ) { //try JRE_HOME if ( verbose ) cout << " - trying JRE_HOME" << endl; javaHome = getenv( "JRE_HOME" ); if ( javaHome != NULL ) { jvm.setJavaHome( javaHome ); if ( jvm.check() ) { jvmFound = true; } } } return jvmFound; } int main ( int argc, char** argv ) { string output = ""; int result = process("dir", output); cout << "dir:" << endl << output << endl << result << endl; JvmProperty jvmProperty = JavaHome; Action action = Find; string arg1 = ""; //parse commandline arguments for (int i=1; i