Changeset 35698 for main/trunk


Ignore:
Timestamp:
2021-10-24T23:44:10+13:00 (2 years ago)
Author:
davidb
Message:

search4j extended to try to find 'java/javac' on PATH after all other ways have failed. Tested on Linux with g++. At this point not clear if dirname() (which is POSIX compliant) is available in VisualStudio.

Location:
main/trunk/search4j
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/search4j/libsearch4j.cpp

    r33930 r35698  
    44
    55#include "libsearch4j.h"
    6 
    76
    87#ifdef WINDOWS
     
    6867
    6968
    70 int process_and_catch_output( string command, string &output ) {
     69int process_and_catch_output( string command, string& output ) {
    7170   
    7271    FILE *pPipe;
     
    9089    /* Read pipe until end of file. */
    9190    while( !feof( pPipe ) ) {
    92         char psBuffer[128];
     91        char psBuffer[512];
    9392        //if ( verbose ) cout << "get some data" << endl;
    94         if( fgets( psBuffer, 128, pPipe ) != NULL ) {
     93        if( fgets( psBuffer, sizeof(psBuffer), pPipe ) != NULL ) {
    9594           
    9695            //if ( verbose ) cout << "got: " << psBuffer << endl;
     
    239238}
    240239
    241 int Jvm::getEra() { return era_; }
    242 int Jvm::getMajor() { return major_; }
    243 int Jvm::getMinor() { return minor_; }
    244 int Jvm::getUpdate() { return update_; }
    245 
    246 bool Jvm::getIsJdk() { return isJdk_; }
    247 bool Jvm::getIsJre() { return !isJdk_; }
     240int  Jvm::getEra()    { return era_; }
     241int  Jvm::getMajor() { return major_; }
     242int  Jvm::getMinor() { return minor_; }
     243int  Jvm::getUpdate() { return update_; }
     244
     245bool Jvm::getIsJdk()  { return isJdk_; }
     246bool Jvm::getIsJre()  { return !isJdk_; }
    248247
    249248void Jvm::setVersionFromExeOuput() {
     
    519518
    520519
     520
     521    if ( !jvmFound ) {     
     522        //look for it on PATH
     523        if ( verbose ) cout << " - Looking on PATH (possibly yielding a pseudo, but passable, JAVA_HOME such as /usr): ";
     524
     525#ifdef WINDOWS
     526        // The following is the DOS equivalent to 'which java', however note that
     527        // 'where' returns a list of all possible matches (one per line)
     528        const char* cmd = "where java 2>nul";
     529#else
     530        const char* cmd = "which java 2>/dev/null";
     531#endif
     532        char full_path_java[512] = { '\0' }; // empty string
     533
     534        // ****
     535        // Consider replacing the following code with a call to process_and_catch_output(command, output)
     536        // but would need extending to have extra (optional) argument 'only_first_list' which defaults to 'false'
     537       
     538        FILE* PIN = popen(cmd,"r");
     539        if (PIN == NULL) {
     540          cerr << "Failed to run the command to locate 'java' on PATH" << endl;
     541          cerr << "Command run was: " << cmd << endl;
     542        }
     543        else {
     544          // Only need the first line of output
     545
     546          // Returns number of chars returned.  OK to ignore here
     547          if (fgets(full_path_java, sizeof(full_path_java), PIN) == NULL) {
     548            // Not strictly necessary (as already initialized to be the empty string when declared)
     549            // but done to void the warning g++ gives about not checking the return value
     550            full_path_java[0] = '\0';
     551          }
     552        }
     553       
     554        // Not all implmentations of 'which' are coded to return 0 if a program match is found
     555        // => safer just to look to see if a non-empty string is returned
     556
     557        int ignore_exit_val = pclose(PIN);
     558       
     559        if ( strcmp(full_path_java,"") != 0) {
     560          // go two directories up from where 'java' was found
     561          javaHomeEnv = dirname(dirname(full_path_java));
     562
     563          // Logic from here same as for other search4j java/javac testing
     564          if ( verbose ) cout << "(" << javaHomeEnv << ") ";
     565          jvm.setJavaHome( javaHomeEnv );
     566          if ( jvm.check() ) {
     567            if ( use_minimum ) {
     568              if ( jvm.compare( minimum ) >= 0 ) {
     569            jvmFound = true;
     570              }
     571            } else {
     572              jvmFound = true;
     573            }
     574          }
     575          if ( (jreOnly && !jvm.getIsJre() ) || (jdkOnly && !jvm.getIsJdk()) ) {
     576            jvmFound = false;
     577          }
     578         
     579        }
     580        if ( verbose ) { if( jvmFound ) cout << "yes" << endl; else cout << "no" << endl; }
     581    }
     582
     583   
    521584    return jvmFound;
    522585}
  • main/trunk/search4j/libsearch4j.h

    r35420 r35698  
    66#include <sstream>
    77#include <sys/stat.h>
     8
     9#include <libgen.h>
     10
    811using namespace std;
    912#ifdef WINDOWS
Note: See TracChangeset for help on using the changeset viewer.