Changeset 7734 for trunk/projects


Ignore:
Timestamp:
08/16/11 15:26:42 (8 years ago)
Author:
hinerm
Message:

More control allowed in JavaTools

The following options were added to JavaTools:

  • specification of one folder containing jars to add to the classpath
  • specification of a list of files/directories to add to the classpath
  • specification of files to add to the java.library.path
  • specifications of commands to add as jace::CustomOptions

The header and method signature options were updated to reflect these changes
Note that with these changes the overall structure of jtools was simplified,
so a top-level directory from which other paths are relative can no longer be specified.

Location:
trunk/projects/jar2lib/src/main/resources/project-files/jtools
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/projects/jar2lib/src/main/resources/project-files/jtools/include/javaTools.h

    r7731 r7734  
    4040 
    4141#include <string> 
     42#include <vector> 
    4243#include <dirent.h> 
    4344#include <iostream> 
     
    6162    /** 
    6263     * This method is used to instantiate a JVM with the following defaults: 
    63      * class path: working directory 
    64      * extra jars: none 
     64     * jarFolder: jar 
    6565     * memory: 256MB 
    6666     * headless: true 
     67     * extraJavaLibraryPath: current working directory 
     68     * extraClasspath: none 
    6769     */ 
    6870    static void createJVM(); 
     
    7173     * This method is used to instantiate a JVM with a custom class path. 
    7274     */ 
    73     static void createJVM(string jarlist); 
     75    static void createJVM(string jarFolder); 
    7476 
    7577    /** 
    7678     * This method is used to instantiate a JVM with custom memory specifications. 
    7779     */ 
    78     static void createJVM(int memory); 
     80    static void createJVM(bool headless); 
    7981 
    8082    /** 
    8183     * This method is used to instantiate a JVM with specified headless mode. 
    8284     */ 
    83     static void createJVM(bool headless); 
     85    static void createJVM(int memory); 
    8486 
    8587    /** 
     
    8789     * memory specifications.. 
    8890     */ 
    89     static void createJVM(string classdir, int memory); 
     91    static void createJVM(bool headless, int memory); 
    9092 
    9193    /** 
     
    9395     * specified headless mode. 
    9496     */ 
    95     static void createJVM(string classdir, bool headless); 
    96  
    97     /** 
    98      * This method is used to instantiate a JVM with extra jars and 
    99      * memory specifications.. 
    100      */ 
    101     static void createJVM(int memory, string jarlist); 
    102  
    103     /** 
    104      * This method is used to instantiate a JVM with extra jars and 
    105      * specified headless mode. 
    106      */ 
    107     static void createJVM(bool headless, string jarlist); 
    108  
    109     /** 
    110      * This method is used to instantiate a JVM with a custom class path and 
    111      * additional jars. 
    112      */ 
    113     static void createJVM(string classdir, string jarlist); 
    114  
    115     /** 
    116      * This method is used to instantiate a JVM with a custom class path, 
    117      * extra jars and custom memory specifications. 
    118      */ 
    119     static void createJVM(string classdir, string jarlist, int memory); 
    120  
    121     /** 
    122      * This method is used to instantiate a JVM with a custom class path, 
    123      * extra jars and specified headless mode. 
    124      */ 
    125     static void createJVM(string classdir, string jarlist, bool headless); 
     97    static void createJVM(string jarFolder, bool headless, int memory); 
    12698 
    12799    /** 
     
    133105     * memory - how much memory to instantiate the JVM with 
    134106     */ 
    135     static void createJVM(string classdir, string jarlist, bool headless, int memory); 
     107    static void createJVM(string jarFolder, bool headless, int memory, vector<string> extraClasspath, vector<string> extraJavaLibraryPath, vector<string> extraOptions); 
    136108}; 
  • trunk/projects/jar2lib/src/main/resources/project-files/jtools/source/javaTools.cxx

    r7731 r7734  
    5151#endif 
    5252 
     53#ifdef __APPLE__ 
     54#define PATH "DYLD_LIBRARY_PATH" 
     55#endif 
     56#ifdef __linux__ 
     57#define PATH "LD_LIBRARY_PATH" 
     58#endif 
     59#ifdef _WIN32 
     60#define PATH "PATH" 
     61#endif 
     62#ifdef _WIN64 
     63#define PATH "PATH" 
     64#endif 
     65 
    5366using namespace std; 
    5467 
    5568void JavaTools::createJVM() 
    5669{ 
    57   JavaTools::createJVM("", "", true, 256); 
    58 } 
    59  
    60 void JavaTools::createJVM(string jarlist) 
    61 { 
    62   JavaTools::createJVM("", jarlist, true, 256); 
     70  JavaTools::createJVM("jar", true, 256, vector<string>(), vector<string>(), vector<string>()); 
     71} 
     72 
     73void JavaTools::createJVM(string jarFolder) 
     74{ 
     75  JavaTools::createJVM(jarFolder, true, 256, vector<string>(), vector<string>(), vector<string>()); 
    6376} 
    6477 
    6578void JavaTools::createJVM(int memory) 
    6679{ 
    67   JavaTools::createJVM("", "", true, memory); 
     80  JavaTools::createJVM("jar", true, memory, vector<string>(), vector<string>(), vector<string>()); 
    6881} 
    6982 
    7083void JavaTools::createJVM(bool headless) 
    7184{ 
    72   JavaTools::createJVM("", "", headless, 256); 
    73 } 
    74  
    75 void JavaTools::createJVM(string classdir, int memory) 
    76 { 
    77   JavaTools::createJVM(classdir, "", true, memory); 
    78 } 
    79  
    80 void JavaTools::createJVM(string classdir, bool headless) 
    81 { 
    82   JavaTools::createJVM(classdir, "", headless, 256); 
    83 } 
    84  
    85 void JavaTools::createJVM(int memory, string jarlist) 
    86 { 
    87   JavaTools::createJVM("", jarlist, true, memory); 
    88 } 
    89  
    90 void JavaTools::createJVM(bool headless, string jarlist) 
    91 { 
    92   JavaTools::createJVM("", jarlist, headless, 256); 
    93 } 
    94  
    95 void JavaTools::createJVM(string classdir, string jarlist) 
    96 { 
    97   JavaTools::createJVM(classdir, jarlist, true, 256); 
    98 } 
    99  
    100 void JavaTools::createJVM(string classdir, string jarlist, int memory) 
    101 { 
    102   JavaTools::createJVM(classdir, jarlist, true, memory); 
    103 } 
    104  
    105 void JavaTools::createJVM(string classdir, string jarlist, bool headless) 
    106 { 
    107   JavaTools::createJVM(classdir, jarlist, headless, 256); 
    108 } 
    109  
    110 //TODO: Add option override java library path 
     85  JavaTools::createJVM("jar", headless, 256, vector<string>(), vector<string>(), vector<string>()); 
     86} 
     87 
     88void JavaTools::createJVM(bool headless, int memory) 
     89{ 
     90  JavaTools::createJVM("jar", headless, memory, vector<string>(), vector<string>(), vector<string>()); 
     91} 
     92 
     93void JavaTools::createJVM(string jarFolder, bool headless, int memory) 
     94{ 
     95  JavaTools::createJVM(jarFolder, headless, memory, vector<string>(), vector<string>(), vector<string>()); 
     96} 
     97 
    11198/** 
    112 * jarlist is a semi-colon-separated list of additional jars to include 
    113 **/ 
    114 void JavaTools::createJVM(string classdir, string jarlist, bool headless, int memory) 
    115 { 
    116  
     99 * jarFolder is the name of the path to a directory of jars to be added to the classpath (default: jar) 
     100 * headless turns headless on or off for JVM instantiation (default: on) 
     101 * memory controls memory for instantiation of JVM in MB (default: 256) 
     102 * extraClasspath is a list of additional classpath entries to include 
     103 * extraJavaLibraryPath is a list of path entries to be appended to the JVM's java.library.path. ( default: .) 
     104 * extraOptions is a list of any other desired arguments to the JVM, added to the jace::OptionList (e.g., "-verbose:jni") 
     105 */ 
     106void JavaTools::createJVM(string jarFolder, bool headless, int memory, 
     107  vector<string> extraClasspath, vector<string> extraJavaLibraryPath, vector<string> extraOptions) 
     108{ 
    117109  try { 
    118110    jace::StaticVmLoader* tmpLoader = (jace::StaticVmLoader*)jace::helper::getVmLoader(); 
     
    123115      jace::StaticVmLoader loader(JNI_VERSION_1_4); 
    124116 
    125       if(classdir.length() >= 1 && classdir.at(classdir.length() - 1) != SLASH ) { 
    126         classdir.append(1,SLASH); 
    127       } 
    128  
    129       std::string classpath (""); 
    130  
    131       classpath += classdir + "jace-runtime.jar"; 
     117      string classpath (""); 
    132118 
    133119      DIR *d; 
    134120      struct dirent *dir; 
    135121 
    136       if(classdir.length() >= 1) 
    137       { 
    138         string tmp_dir(classdir); 
    139         tmp_dir += "jar"; 
    140         d = opendir(tmp_dir.c_str()); 
     122//TODO: test jar/* 
     123      // All .jar files in the jarFolder are added to the JVM's classpath 
     124      d = opendir(jarFolder.c_str()); 
     125 
     126      if(d) 
     127      { 
     128        while ((dir = readdir(d)) != NULL) 
     129        { 
     130          string tmpName(dir->d_name); 
     131 
     132          if(tmpName.find(".jar") != string::npos) 
     133          { 
     134            if(classpath.length() >= 1) 
     135            { 
     136              classpath += PATHSTEP; 
     137            } 
     138            classpath += jarFolder + SLASH + dir->d_name; 
     139          } 
     140        } 
     141 
     142        closedir(d); 
     143      } 
     144 
     145      // All items in the extraClasspath are added to the JVM's classpath 
     146      if(!extraClasspath.empty()) 
     147      { 
     148        for(vector<string>::iterator it = extraClasspath.begin(); it != extraClasspath.end(); it++) 
     149        { 
     150          classpath += PATHSTEP; 
     151          classpath += *it; 
     152        } 
     153      } 
     154 
     155      //std::cout << "extraClasspath : " << extraClasspath << std::endl; 
     156      //std::cout << "Classpath for JVM: " << classpath << std::endl; 
     157 
     158      // Gets the system-specific path environment variable's contents (which will eventually be passed to java.library.path in this JVM) 
     159      std::string pName = PATH; 
     160      char * libPath; 
     161      libPath = getenv(pName.c_str()); 
     162      std::string jLibPath(""); 
     163      if(libPath != NULL) 
     164      { 
     165        jLibPath = libPath; 
     166      } 
     167 
     168      // If no extra Java library path is specified, the current working directory is appended and passed to java.library.path. 
     169      // Otherwise, each provided path is appended. 
     170      if(extraJavaLibraryPath.empty()) 
     171      { 
     172        if(jLibPath.length() >= 1) 
     173        { 
     174          jLibPath += PATHSTEP; 
     175        } 
     176        jLibPath += '.'; 
    141177      } 
    142178      else 
    143179      { 
    144         d = opendir("./jar"); 
    145       } 
    146  
    147       if(d) 
    148       { 
    149         while ((dir = readdir(d)) != NULL) 
    150         { 
    151           string tmp_name(dir->d_name); 
    152  
    153           if(tmp_name.compare(".") != 0 && tmp_name.compare("..") !=0) 
     180        for(vector<string>::iterator it = extraJavaLibraryPath.begin(); it != extraJavaLibraryPath.end(); it++) 
     181        { 
     182          if(jLibPath.length() >= 1) 
    154183          { 
    155             classpath += PATHSTEP; 
    156             classpath += classdir + "jar" + SLASH + dir->d_name; 
     184           jLibPath += PATHSTEP; 
    157185          } 
    158         } 
    159  
    160         closedir(d); 
    161       } 
    162  
    163  
    164       if(jarlist.length() >= 1) 
    165       { 
    166         classpath += PATHSTEP; 
    167  
    168         size_t found; 
    169         found = jarlist.find_first_of(";"); 
    170         while(found != string::npos) 
    171         { 
    172           classpath += classdir + jarlist.substr(0, found) + PATHSTEP; 
    173  
    174           jarlist = jarlist.substr(found + 1, jarlist.length()); 
    175  
    176           found = jarlist.find_first_of(";"); 
    177         } 
    178  
    179         classpath += classdir + jarlist.substr(0, found); 
    180       } 
    181  
    182       //std::cout << "jarlist : " << jarlist << std::endl; 
    183       //std::cout << "Classpath for JVM: " << classpath << std::endl; 
    184  
    185       list.push_back(jace::ClassPath( 
    186       classpath 
    187       )); 
     186          jLibPath += *it; 
     187        } 
     188      } 
     189 
     190      list.push_back(jace::ClassPath(classpath)); 
    188191      list.push_back(jace::CustomOption("-Xcheck:jni")); 
    189192 
     193      // Int to string conversion 
    190194      std::string mem; 
    191195      std::stringstream out; 
     
    196200 
    197201      if (headless) 
     202      { 
    198203        list.push_back(jace::CustomOption("-Djava.awt.headless=true")); 
    199       //list.push_back(jace::CustomOption("-verbose")); 
    200       //list.push_back(jace::CustomOption("-verbose:jni")); 
     204      } 
     205 
     206      list.push_back(jace::CustomOption("-Djava.library.path=" + jLibPath)); 
     207 
     208      // All extra options are added as CustomOptions for JVM instantiation 
     209      if (!extraOptions.empty()) { 
     210        for (vector<string>::iterator it = extraOptions.begin(); it != extraOptions.end(); it++) 
     211        { 
     212          list.push_back(jace::CustomOption(*it)); 
     213        } 
     214      } 
     215 
    201216      jace::helper::createVm(loader, list, false); 
    202217    } 
Note: See TracChangeset for help on using the changeset viewer.