source: trunk/projects/jar2lib/src/main/resources/project-files/jtools/source/javaTools.cxx @ 7718

Revision 7718, 5.2 KB checked in by hinerm, 9 years ago (diff)
  • Updated javaTools with future plans
  • Fixed class path bug
RevLine 
[7713]1//
2// javaTools.cxx
3//
4
5/*
6OME Bio-Formats ITK plugin for calling Bio-Formats from the Insight Toolkit.
7Copyright (c) 2008-@year@, UW-Madison LOCI.
8All rights reserved.
9
10Redistribution and use in source and binary forms, with or without
11modification, are permitted provided that the following conditions are met:
12    * Redistributions of source code must retain the above copyright
13      notice, this list of conditions and the following disclaimer.
14    * Redistributions in binary form must reproduce the above copyright
15      notice, this list of conditions and the following disclaimer in the
16      documentation and/or other materials provided with the distribution.
17    * Neither the name of the UW-Madison LOCI nor the
18      names of its contributors may be used to endorse or promote products
19      derived from this software without specific prior written permission.
20
21THIS SOFTWARE IS PROVIDED BY UW-MADISON LOCI ''AS IS'' AND ANY
22EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24DISCLAIMED. IN NO EVENT SHALL UW-MADISON LOCI BE LIABLE FOR ANY
25DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31*/
32
33/*
34IMPORTANT NOTE: Although this software is distributed according to a
35"BSD-style" license, it requires the OME Bio-Formats Java library to do
36anything useful, which is licensed under the GPL v2 or later.
37As such, if you wish to distribute this software with Bio-Formats itself,
38your combined work must be distributed under the terms of the GPL.
39*/
40#include <iostream>
41#include <string>
42#include <sstream>
43#include "javaTools.h"
44
45#if defined (_WIN32)
46#define PATHSTEP ';'
47#define SLASH '\\'
48#else
49#define PATHSTEP ':'
50#define SLASH '/'
51#endif
52
53using namespace std;
54
55void JavaTools::createJVM()
56{
57  JavaTools::createJVM("", "", true, 256);
58}
59
[7718]60void JavaTools::createJVM(string jarlist)
[7713]61{
[7718]62  JavaTools::createJVM("", jarlist, true, 256);
[7713]63}
64
65void JavaTools::createJVM(int memory)
66{
67  JavaTools::createJVM("", "", true, memory);
68}
69
70void JavaTools::createJVM(bool headless)
71{
72  JavaTools::createJVM("", "", headless, 256);
73}
74
75void JavaTools::createJVM(string classdir, int memory)
76{
[7718]77  JavaTools::createJVM(classdir, "", true, memory);
[7713]78}
79
80void JavaTools::createJVM(string classdir, bool headless)
81{
[7718]82  JavaTools::createJVM(classdir, "", headless, 256);
[7713]83}
84
85void JavaTools::createJVM(int memory, string jarlist)
86{
87  JavaTools::createJVM("", jarlist, true, memory);
88}
89
90void JavaTools::createJVM(bool headless, string jarlist)
91{
[7718]92  JavaTools::createJVM("", jarlist, headless, 256);
[7713]93}
94
95void JavaTools::createJVM(string classdir, string jarlist)
96{
97  JavaTools::createJVM(classdir, jarlist, true, 256);
98}
99
100void JavaTools::createJVM(string classdir, string jarlist, int memory)
101{
102  JavaTools::createJVM(classdir, jarlist, true, memory);
103}
104
105void JavaTools::createJVM(string classdir, string jarlist, bool headless)
106{
107  JavaTools::createJVM(classdir, jarlist, headless, 256);
108}
109
[7718]110//TODO: Add option override java library path
[7713]111void JavaTools::createJVM(string classdir, string jarlist, bool headless, int memory)
112{
113
114  try {
115    jace::StaticVmLoader* tmpLoader = (jace::StaticVmLoader*)jace::helper::getVmLoader();
116    if(tmpLoader == NULL) {
117
118      // initialize the Java virtual machine
119      jace::OptionList list;
120      jace::StaticVmLoader loader(JNI_VERSION_1_4);
121
122      if(classdir.length() >= 1 && classdir.at(classdir.length() - 1) != SLASH ) {
123        classdir.append(1,SLASH);
124      }
125
126      std::string classpath ("");
127
[7718]128      //TODO: Add all Jar2Lib classpath jars to this list by default (by template? by txt file?)
129      classpath += classdir + "jace-runtime.jar";
[7713]130
131      if(jarlist.length() >= 1)
132      {
133        classpath += PATHSTEP;
134
135        size_t found;
136        found = jarlist.find_first_of(";");
137        while(found != string::npos)
138        {
139          classpath += classdir + jarlist.substr(0, found) + PATHSTEP;
140
141          jarlist = jarlist.substr(found + 1, jarlist.length());
142
143          found = jarlist.find_first_of(";");
144        }
145
146        classpath += classdir + jarlist.substr(0, found);
147      }
148
[7718]149      std::cout << "jarlist : " << jarlist << std::endl;
150
151      std::cout << "Classpath for JVM: " << classpath << std::endl;
152
[7713]153      list.push_back(jace::ClassPath(
154      classpath
155      ));
156      list.push_back(jace::CustomOption("-Xcheck:jni"));
157
158      std::string mem;
159      std::stringstream out;
160      out << memory;
161      mem = out.str();
162
163      list.push_back(jace::CustomOption("-Xmx" + mem + "m"));
164
165      if (headless)
166        list.push_back(jace::CustomOption("-Djava.awt.headless=true"));
167      //list.push_back(jace::CustomOption("-verbose"));
168      //list.push_back(jace::CustomOption("-verbose:jni"));
169      jace::helper::createVm(loader, list, false);
170    }
171  }
172  catch (jace::JNIException& jniException) {
173    std::cerr << "Exception creating JVM: " << jniException.what() << std::endl;
174  }
175}
Note: See TracBrowser for help on using the repository browser.