def attach(logger=None, heap_space='6g'): if logger is not None: logger.debug("JVM Started: {}".format(jp.isJVMStarted())) if not jp.isJVMStarted(): path_to_package = bayesianpy.utils.get_path_to_parent_dir(__file__) separator = ";" if platform.system() == "Linux": separator = ":" jars = [ 'bin/bayesserver-{}.jar'.format(BAYES_SERVER_VERSION), 'bin/sqlite-jdbc-3.8.11.2.jar', 'bin/mysql-connector-java-5.0.8-bin.jar', 'bin/jaybird-full-3.0.2.jar' ] classpath = ".{}".format(separator) for jar in jars: if os.path.exists(os.path.join(path_to_package, jar)): classpath += "{}{}".format(os.path.join(path_to_package, jar), separator) if logger is not None: logger.debug("Starting JVM ({})...".format(classpath)) jp.startJVM(jp.getDefaultJVMPath(), "-Djava.class.path={}".format(classpath), "-XX:-UseGCOverheadLimit", "-Xmx{}".format(heap_space)) if logger is not None: logger.debug("JVM Started.") # so it doesn't crash if called by a Python thread. attach_thread(logger)
def start_jvm(self, jar_list): # 获得默认jvm路径 jvm_path = jpype.getDefaultJVMPath() # self.log.info(jvm_path) # 你的java扩展包的路径 ext_classpath = '' # 判断系统类型,Linux系统使用“ :”分隔 sysstr = platform.system() if sysstr == "Windows": # java扩展包的路径或class文件所在文件夹路径,注意:class文件路径是它所在的上级文件夹 ext_classpath = config.sdk_path for name in jar_list: ext_classpath += ';' + config.sdk_path + '%s' % name elif sysstr == "Linux": ext_classpath = config.sdk_path + 'sdk' for name in jar_list: ext_classpath = ':' + config.sdk_path + '%s' % name # self.log.info("系统类型:" + sysstr) # 判断 JVM 是否已启动 if not jpype.isJVMStarted(): # 启动Java虚拟机,并加载jar包 jpype.startJVM(jvm_path, '-ea', '-Djava.class.path=%s' % ext_classpath) jpype.java.lang.System.out.println("startJVM success!") if jpype.isJVMStarted(): return True else: return False else: return True
def rmi_example(): if not jpype.isJVMStarted(): try: jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=/path/to/classes") except TypeError as ex: print("TypeError raised: {}.".format(ex)) except OSError as ex: print("OSError raised: {}.".format(ex)) #-------------------- import java.rmi try: p = java.rmi.Naming.lookup("rmi://localhost:2004/server") except java.rmi.ConnectException as ex: print("java.rmi.ConnectException raised: {}.".format(ex)) return print(p, p.__class__) p.callRemote() #-------------------- if jpype.isJVMStarted(): jpype.shutdownJVM()
def _start_jvm(self, additional_flags: Optional[List[str]]): flags = ['-Djava.class.path={0}'.format(self._classpath)] if additional_flags: flags.extend(additional_flags) logging.info('jpype.isJVMStarted(): {0}'.format(jpype.isJVMStarted())) if not jpype.isJVMStarted(): jpype.startJVM(jpype.getDefaultJVMPath(), *flags)
def batch_send_sms(user_list, smsData): #获取jar包存放路径 jarpath = "./release/" #jvm路径 #jvmpath=jpype.getDefaultJVMPath() jvmpath = "/usr/lib/jvm//jre/lib/amd64/server/libjvm.so" #待使用jar包文件 jarFile = 'SMSGatewayMidwarezxc.jar' #jarBase='SMSGatewayMidwareBase.jar' try: #检测并启动jvm,get init args first if not jpype.isJVMStarted(): #test whether the JVM is started logging.error('jvm is not start!') jpype.startJVM(jvmpath, "-ea", "-Djava.class.path=%s" % (jarpath + jarFile)) while True: if jpype.isJVMStarted(): break logging.error("jvm has started!") if jpype.isJVMStarted(): logging.error('jvm is start!time is %s', str(time.time())) #get ipv4 and type IPV4Util = jpype.JClass("com.cisi.TWBase.IPV4Util") KSystemType = jpype.JClass("com.cisi.TWBase.KSystemType") #get args systemtype = KSystemType.EUserManageSystem smsgateway = config.get('smsgate_host') #gate ip smsgate = IPV4Util.ipToInt(smsgateway) smsport = int(10320) # logging.error("three init arg is : %s , %s and %s , and ip is : %s",systemtype,smsgate,smsport,smsgateway) #create the Java class SMSManager = jpype.JClass("com.cisi.client.midware.SMSManager") smsmanager = SMSManager() if not smsmanager.Init(systemtype, smsgate, smsport): # logging.error('init result is: %s',smsmanager.Init(systemtype,smsgate,smsport)) raise logging.error('init succeed!time is %s', str(time.time())) for phoneNumber in user_list: #发送短信 if not check_pnumber(phoneNumber): logging.error("phone number is not valid,pnumber:%s", phoneNumber) logging.error('check number succeed!time is %s', str(time.time())) sendresult = smsmanager.SendOtherSMS(phoneNumber, smsData) logging.error('sms is sended! the result is %s,time is %s', sendresult, str(time.time())) #+++ 20150612 if sendresult == -6030: logging.error("send sms failed,%s", phoneNumber) except Exception, ex: logging.error('send_sms. ex:%s', ex) return False
def init(self): jarpath="./release/" #jvm路径 jvmpath=jpype.getDefaultJVMPath() # jvmpath="/usr/lib/jvm//jre/lib/amd64/server/libjvm.so" #待使用jar包文件 jarFile='SMSGatewayMidwarezxc.jar' # smsmanager = None try: #检测并启动jvm,get init args first if not jpype.isJVMStarted(): #test whether the JVM is started logging.error('jvm is not start!') jpype.startJVM(jvmpath,"-ea","-Djava.class.path=%s" % (jarpath+jarFile)) while True: if jpype.isJVMStarted(): break logging.error("jvm has started!") if jpype.isJVMStarted(): # if not jpype.isThreadAttachedToJVM(): # jpype.attachThreadToJVM() # +++20150915 必须加入这个方法才能在jpype环境下使用多线程 logging.error('jvm is start!%s',time.time()) #get ipv4 and type IPV4Util = jpype.JClass("com.cisi.TWBase.IPV4Util") KSystemType = jpype.JClass("com.cisi.TWBase.KSystemType") logging.error('get ipv4 and type%s',time.time()) #get args systemtype=KSystemType.EUserManageSystem # smsgateway=config.get('smsgate_host') #gate ip smsgateway="122.13.138.180" smsgate=IPV4Util.ipToInt(smsgateway) smsport=int(10320) logging.error('smsport%s',time.time()) #create the Java class SMSManager = jpype.JClass("com.cisi.client.midware.SMSManager") logging.error('SMSManager%s',time.time()) smsmanager=SMSManager() initResult = smsmanager.Init(systemtype,smsgate,smsport) if not initResult: logging.error('init result is: %s,time is %s',initResult,time.time()) raise logging.error('init succeed!') logging.error(str(smsmanager)) self.smsmanager = smsmanager logging.error(str(self.smsmanager)) # smsmanager = jpype.JClass("ucas.gcy.sendMessage") except Exception as e: print e.message self.smsmanager = None
def __init__(self, jvmpath=None): if not jpype.isJVMStarted(): jvm.init_jvm(jvmpath) tktJavaPackage = jpype.JPackage('kr.lucypark.tkt') TktInterfaceJavaClass = tktJavaPackage.TktInterface self.jki = TktInterfaceJavaClass()
def check(self, textkey, data, config): try: import jpype from jpype import java, javax if not jpype.isJVMStarted(): jpype.startJVM(config['jvm_path']) jhash = java.util.HashMap() jarray = jpype.JArray(java.lang.String)([config['username'], config['password']]) jhash.put(javax.management.remote.JMXConnector.CREDENTIALS, jarray) url = "service:jmx:rmi:///jndi/rmi://%s:%d/jmxrmi" % (config['host'], int(config['port'])) jmxurl = javax.management.remote.JMXServiceURL(url) jmxsoc = javax.management.remote.JMXConnectorFactory.connect(jmxurl, jhash) connection = jmxsoc.getMBeanServerConnection() parts = data.split("/", 3) obj = parts[0] attribute = parts[1] if len(parts) == 3: key = parts[2] else: key = None res = connection.getAttribute(javax.management.ObjectName(obj), attribute) if key: res = res.contents.get(key) return res.floatValue() except: return 0
def __init__(self, class_schema_definition_dicts={}, class_abrv_lookup={}, \ use_weka_jvm=True, training_arff_features_list=[]): self.class_schema_definition_dicts = class_schema_definition_dicts self.training_arff_features_list = training_arff_features_list self.arffmaker = arffify.Maker(search=[], \ skip_class=True, local_xmls=True, dorun=False, \ class_abrv_lookup=class_abrv_lookup) if use_weka_jvm: # TODO/NOTE: I think a WekaClassifier() class needs to be # instantiated for each WEKA classification instance # which uses a different .model and/or training .arff # We initialize a Java virtual machine for Weka classifications #try: if not jpype.isJVMStarted(): #TODO / DEBUG: disable the next line for speed-ups once stable? _jvmArgs = ["-ea"] # enable assertions _jvmArgs.append("-Djava.class.path=" + \ os.environ["CLASSPATH"]) ###20091905 dstarr comments out: #_jvmArgs.append("-Xmx1000m") _jvmArgs.append("-Xmx12000m") # 4000 & 5000m works, 3500m doesnt for some WEKA .models jpype.startJVM(jpype.getDefaultJVMPath(), *_jvmArgs) class_schema_name_list = self.class_schema_definition_dicts.keys() class_schema_name_list.remove('mlens3 MicroLens') class_schema_name_list.remove('Dovi SN') class_schema_name_list.remove('General') self.wc = {} for class_schema_name in class_schema_name_list: class_schema_dict = self.class_schema_definition_dicts[class_schema_name] weka_training_model_fpath = class_schema_dict['weka_training_model_fpath'] weka_training_arff_fpath = class_schema_dict['weka_training_arff_fpath'] self.wc[class_schema_name] = weka_classifier.WekaClassifier( \ weka_training_model_fpath, weka_training_arff_fpath)
def init_jvm(jvmpath=None): """Initializes the Java virtual machine (JVM). :param jvmpath: The path of the JVM. If left empty, inferred by :py:func:`jpype.getDefaultJVMPath`. """ if jpype.isJVMStarted(): logging.warning('JVM is already running. Do not init twice!') return folder_suffix = ['{0}', '{0}{1}bin',\ '{0}{1}jhannanum-0.8.4.jar',\ '{0}{1}kkma-2.0.jar',\ '{0}{1}komoran-2.4-e.jar',\ '{0}{1}shineware-common-2.0.jar', '{0}{1}shineware-ds-1.0.jar',\ '{0}{1}snakeyaml-1.12.jar', '{0}{1}scala-library-2.11.4.jar', '{0}{1}twitter-korean-text-2.4.3.jar', '{0}{1}twitter-text-1.10.1.jar',\ '{0}{1}*'] javadir = '%s%sjava' % (utils.installpath, os.sep) args = [javadir, os.sep] classpath = os.pathsep.join(f.format(*args) for f in folder_suffix) jvmpath = jvmpath or jpype.getDefaultJVMPath() if jvmpath: jpype.startJVM(jvmpath, '-Djava.class.path=%s' % classpath,\ '-Dfile.encoding=UTF8',\ '-ea', '-Xmx768m') else: raise ValueError("Please specify the JVM path.")
def jvm(): """ Return the jpype module, after guaranteeing the JVM is running and the classpath has been loaded properly. """ jarpaths = (os.path.abspath( os.path.join(os.path.dirname(__file__), "../dist")), '/usr/share/java') if not jpype.isJVMStarted(): max_mem = get_jvm_max_mem() jpype.startJVM(jpype.getDefaultJVMPath(), "-Xmx%sM" % max_mem, "-Djava.ext.dirs=%s:%s" % jarpaths, # setting Schema path here is ugly, but it's better than # doing it before all XML parsing calls "-Dopenquake.nrml.schema=%s" % nrml.nrml_schema_file(), # force the default Xerces parser configuration, otherwise # some random system-installed JAR might override it "-Dorg.apache.xerces.xni.parser.XMLParserConfiguration=" \ "org.apache.xerces.parsers.XIncludeAwareParserConfiguration") _init_logs() return jpype
def _jdbc_connect_jpype(jclassname, *args): import jpype # Ensure JVN is running if not jpype.isJVMStarted(): jpype.startJVM(jpype.getDefaultJVMPath()) # Connect Jpype to JVM Thread if not jpype.isThreadAttachedToJVM(): jpype.attachThreadToJVM() # Initialize Type Conversion Mapping if _converters is None: types = jpype.java.sql.Types types_map = {} for i in types.__javaclass__.getClassFields(): types_map[i.getName()] = i.getStaticAttribute() _init_converters(types_map) global _java_array_byte if _java_array_byte is None: def _java_array_byte(data): return jpype.JArray(jpype.JByte, 1)(data) # register driver for DriverManager jpype.JClass(jclassname) return jpype.java.sql.DriverManager.getConnection(*args)
def read_col_oracle(con_str, table_name): jdbc_driver_name = 'oracle.jdbc.OracleDriver' jdbc_driver_loc = './jdbc/ojdbc6.jar' url = 'jdbc:oracle:thin:@10.54.9.105:4713/TRACEDEV' username = '' password = '' args = '-Djava.class.path=%s' % jdbc_driver_loc jvm = jpype.getDefaultJVMPath() if not jpype.isJVMStarted(): jpype.startJVM(jvm) conn_ora = jaydebeapi.connect(jdbc_driver_name, [url, username, password], jdbc_driver_loc) curs_ora = conn_ora.cursor() print table_name query_column_info = "select COLUMN_NAME, DATA_TYPE, DATA_LENGTH, NULLABLE FROM USER_TAB_COLUMNS where table_name = '%s' ORDER BY COLUMN_ID" % ( table_name.rstrip()) curs_ora.execute(query_column_info) column_info = [] val = curs_ora.fetchall() column_info.append(table_name.rstrip()) for column_name_info in val: column_info.append(column_name_info) return column_info
def __init__(self): self.pdfbox_path = self._get_pdfbox_path() jpype.addClassPath(self.pdfbox_path) if not jpype.isJVMStarted(): jpype.startJVM(convertStrings=False) import org.apache.pdfbox.tools as tools self.pdfbox_tools = tools
def GetNoun(self, page): if self.debug: print("GetNoun() started...") self.buff = "" self.tag_count_list = [] self.tags_list = [] with urllib.request.urlopen(page) as self.response: self.soup = BeautifulSoup(self.response.read(), "html.parser") self.nouns = self.soup.select("p") for self.noun in self.nouns: self.buff += self.noun.text path = jpype.getDefaultJVMPath() if jpype.isJVMStarted(): jpype.attachThreadToJVM() else: jpype.startJVM(path) self.nouns = okt.nouns(self.buff) self.count = Counter(self.nouns) for i, j in self.count.most_common(): dicts = {"tag": i, "count": j} self.tag_count_list.append(dicts) self.tags_list.append(dicts['tag']) if self.debug: print("GetNoun() finished...\n") return self.tag_count_list
def validate(request): """ Handle Validate api request """ if request.method == 'GET': """ Return all validate api request """ query = ValidateFileUpload.objects.all() serializer = ValidateSerializer(query, many=True) return Response(serializer.data) elif request.method == 'POST': """ Return validate tool result on the post file""" serializer = ValidateSerializer(data=request.data) if serializer.is_valid(): if (jpype.isJVMStarted() == 0): """ If JVM not already started, start it, attach a Thread and start processing the request """ classpath = abspath(".") + "/tool.jar" jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % classpath) """Attach a Thread and start processing the request """ jpype.attachThreadToJVM() package = jpype.JPackage("org.spdx.tools") verifyclass = package.Verify try: if request.FILES["file"]: """ Saving file to the media directory """ myfile = request.FILES['file'] folder = "api/" + str(request.user) + "/" + str(int( time())) fs = FileSystemStorage( location=settings.MEDIA_ROOT + "/" + folder, base_url=urljoin(settings.MEDIA_URL, folder + '/')) filename = fs.save(myfile.name, myfile) uploaded_file_url = fs.url(filename) """ Call the java function with parameter""" retval = verifyclass.verify(settings.APP_DIR + uploaded_file_url) if (len(retval) > 0): result = "The following error(s)/warning(s) were raised: " + str( retval) returnstatus = status.HTTP_400_BAD_REQUEST httpstatus = 400 jpype.detachThreadFromJVM() else: result = "This SPDX Document is valid." returnstatus = status.HTTP_201_CREATED httpstatus = 201 jpype.detachThreadFromJVM() else: result = "File Not Uploaded" returnstatus = status.HTTP_400_BAD_REQUEST httpstatus = 400 jpype.detachThreadFromJVM() except jpype.JavaException, ex: """ Error raised by verifyclass.verify without exiting the application""" result = jpype.JavaException.message( ex ) #+ "This SPDX Document is not a valid RDF/XML or tag/value format" returnstatus = status.HTTP_400_BAD_REQUEST httpstatus = 400 jpype.detachThreadFromJVM() except:
def __init__(self, jar_filename=None, download_if_missing=False, version=None, extra_jvm_args=None, start_jpype=True, jvm_path=None): """extra_jvm_args can be set to a list of strings which will be passed to your JVM. If start_jpype is True, we will start a JVM via JPype if one hasn't been started already. The user is responsible for stopping the JVM (jpype.shutdownJVM()) when they are done converting. Once the JVM has been shutdown, you'll need to create a new JPypeBackend in order to convert after that. jvm_path is the path to libjvm.so (if None, will use JPype's default JRE path).""" StanfordDependencies.__init__(self, jar_filename, download_if_missing, version) if start_jpype and not jpype.isJVMStarted(): jpype.startJVM(jvm_path or jpype.getDefaultJVMPath(), '-ea', '-Djava.class.path=' + self.jar_filename, *(extra_jvm_args or [])) self.corenlp = jpype.JPackage('edu').stanford.nlp try: self.acceptFilter = self.corenlp.util.Filters.acceptFilter() except TypeError: # this appears to be caused by a mismatch between CoreNLP and JRE # versions since this method changed to return a Predicate. version = jpype.java.lang.System.getProperty("java.version") self._report_version_error(version) trees = self.corenlp.trees self.treeReader = trees.Trees.readTree self.converter = trees.EnglishGrammaticalStructure self.universal_converter = trees.UniversalEnglishGrammaticalStructure # we now need to test whether we can actually create a universal # converter -- we'll call it with invalid number of arguments # since we don't want create a tree just for this try: self.universal_converter() except TypeError: # this is JPype's way of saying that it doesn't exist so we # fall back to the original converter self.universal_converter = self.converter except RuntimeError as re: # this means it exists but wanted a different number of arguments # (in other words, we have a universal converter) assert "No matching overloads found" in str(re) try: self.stemmer = \ self.corenlp.process.Morphology.stemStaticSynchronized except AttributeError: # stemStaticSynchronized was renamed in CoreNLP 3.6.0 to stemStatic self.stemmer = \ self.corenlp.process.Morphology.stemStatic puncFilterInstance = trees.PennTreebankLanguagePack(). \ punctuationWordRejectFilter() try: self.puncFilter = puncFilterInstance.test except AttributeError: self.puncFilter = puncFilterInstance.accept self.lemma_cache = {}
def __init__(self, jvmpath=None): if not jpype.isJVMStarted(): jvm.init_jvm(jvmpath) jhannanumJavaPackage = jpype.JPackage('kr.lucypark.jhannanum.comm') HannanumInterfaceJavaClass = jhannanumJavaPackage.HannanumInterface self.jhi = HannanumInterfaceJavaClass() # Java instance
def open(cls, connection): if connection.state == 'open': logger.debug('Connection is already open, skipping open.') return connection credentials = cls.get_credentials(connection.credentials) try: if jpype.isJVMStarted() and not jpype.isThreadAttachedToJVM(): jpype.attachThreadToJVM() jpype.java.lang.Thread.currentThread().setContextClassLoader(jpype.java.lang.ClassLoader.getSystemClassLoader()) C = jaydebeapi.connect('com.ibm.db2.jcc.DB2Driver', 'jdbc:db2://' + credentials.host + ':' + str(credentials.port) + '/' + credentials.database, [credentials.username, credentials.password], 'C:/Users/ilija/Downloads/db2jcc-db2jcc4.jar') connection.handle = C connection.state = 'open' except Exception as e: logger.debug("Got an error when attempting to open a postgres " "connection: '{}'" .format(e)) connection.handle = None connection.state = 'fail' raise dbt.exceptions.FailedToConnectException(str(e)) return connection
def register_java_class_path(class_path): """ Add a new classpath to the set of class-paths for the JVM. You need to do this before the JVM is started (which happends when you use JPypeConnection (see below), so generally, you want to call this function at import-time. path can be: A list, in which case each element is taken to be a path A string, consisting either of a single path or a set of paths separated by ":" """ assert not jp.isJVMStarted( ), "Too late! JVM is already started. You need to register class paths before starting the JVM (do it on import)." if isinstance(class_path, str): class_path = class_path.split(':') else: assert isinstance( class_path, (list, tuple)), "class_path should either be a string or a list/tuple" for cp in class_path: assert os.path.exists( cp ), "Java Class path: %s does not exist. Did you forget to compile the Java code?" % ( cp, ) global _JAVA_CLASS_PATH _JAVA_CLASS_PATH += class_path
def jvm(max_mem=None): """Return the jpype module, after guaranteeing the JVM is running and the classpath has been loaded properly.""" jarpaths = (os.path.abspath( os.path.join(os.path.dirname(__file__), "../lib")), os.path.abspath( os.path.join(os.path.dirname(__file__), "../dist"))) # TODO(JMC): Make sure these directories exist # LOG.debug("Jarpath is %s", jarpaths) if not jpype.isJVMStarted(): max_mem = get_jvm_max_mem(max_mem) LOG.debug("Default JVM path is %s" % jpype.getDefaultJVMPath()) jpype.startJVM( jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s:%s" % jarpaths, #"-Dnet.spy.log.LoggerImpl=net.spy.memcached.compat.log.Log4JLogger", # "-Dlog4j.debug", "-Dlog4j.configuration=log4j.properties", "-Dlog4j.rootLogger=%s, A1" % (FLAGS.debug.upper()), # "-Dlog4j.rootLogger=DEBUG, A1", "-Xmx%sM" % max_mem) if FLAGS.capture_java_debug: mystream = jpype.JProxy("org.gem.IPythonPipe", inst=sys.stdout) errstream = jpype.JProxy("org.gem.IPythonPipe", inst=sys.stderr) outputstream = jpype.JClass("org.gem.PythonOutputStream")() err_stream = jpype.JClass("org.gem.PythonOutputStream")() outputstream.setPythonStdout(mystream) err_stream.setPythonStdout(errstream) ps = jpype.JClass("java.io.PrintStream") jpype.java.lang.System.setOut(ps(outputstream)) jpype.java.lang.System.setErr(ps(err_stream)) return jpype
def generate(content, engine, name): #dir_name,uuidx = Util.set_dir() dir_tmp = tempfile.TemporaryDirectory() uuidx = uuid.uuid4().__str__() dir_name = dir_tmp.name swagger_file_path = os.path.join(dir_name, uuidx) swagger_file = open(swagger_file_path, "w+") swagger_file.write(content) swagger_file.close() #startJVM(getDefaultJVMPath(),"-ea", "-Djava.class.path=halo-flask-openapi-generator.jar;openapi-generator-cli.jar") if not jpype.isJVMStarted(): #@todo make sure running directory is project root jars_dir = os.path.join(settings.BASE_DIR, '..', 'jars') jars_path = jars_dir + '/*' #jar_dir = os.path.join(os.getcwd(), 'jars') print("starting jvm:" + jars_path) startJVM("-ea", classpath=[jars_path], convertStrings=False) print(java.lang.System.getProperty('java.class.path')) addClassPath("org/openapitools/codegen") #from org.openapitools.codegen import OpenAPIGenerator jpype.JClass("org.openapitools.codegen.OpenAPIGenerator").main([ 'generate', '-g', engine, '-i', swagger_file_path, "-o", dir_name, "--package-name", name ]) print("finished generate") return Util.build_result(dir_name, name)
def jvm(max_mem=4000): """Return the jpype module, after guaranteeing the JVM is running and the classpath has been loaded properly.""" jarpaths = (os.path.abspath( os.path.join(os.path.dirname(__file__), "../lib")), os.path.abspath( os.path.join(os.path.dirname(__file__), "../dist"))) # TODO(JMC): Make sure these directories exist # LOG.debug("Jarpath is %s", jarpaths) if not jpype.isJVMStarted(): LOG.debug("Default JVM path is %s" % jpype.getDefaultJVMPath()) jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s:%s" % jarpaths, #"-Dnet.spy.log.LoggerImpl=net.spy.memcached.compat.log.Log4JLogger", # "-Dlog4j.debug", "-Dlog4j.configuration=log4j.properties", "-Dlog4j.rootLogger=%s, A1" % (FLAGS.debug.upper()), # "-Dlog4j.rootLogger=DEBUG, A1", "-Xmx%sM" % max_mem) if FLAGS.capture_java_debug: mystream = jpype.JProxy("org.gem.IPythonPipe", inst=sys.stdout) errstream = jpype.JProxy("org.gem.IPythonPipe", inst=sys.stderr) outputstream = jpype.JClass("org.gem.PythonOutputStream")() err_stream = jpype.JClass("org.gem.PythonOutputStream")() outputstream.setPythonStdout(mystream) err_stream.setPythonStdout(errstream) ps = jpype.JClass("java.io.PrintStream") jpype.java.lang.System.setOut(ps(outputstream)) jpype.java.lang.System.setErr(ps(err_stream)) return jpype
def cleanup(): """ Cleans up resources at the end of the Python session. This function is not part of the public API. It runs automatically at the end of the Python session and is not intended to be called directly from application code. Stops the local server instance possibly created by `start()` and shuts down the Java Virtual Machine hosting the client instance. """ if client and client.port: try: client.disconnect() except Exception: error = 'Error while disconnecting client at session clean-up.' logger.exception(error) if server and server.running(): server.stop() if jpype.isJVMStarted(): logger.info('Exiting the Java virtual machine.') sys.stdout.flush() sys.stderr.flush() jpype.java.lang.Runtime.getRuntime().exit(exit_code) # No code is reached from here on due to the hard exit of the JVM. logger.info('Java virtual machine has exited.')
def post(self): import jpype if jpype.isJVMStarted(): jpype.attachThreadToJVM() result = [] corpus = self.params['corpus'] if corpus: result_count = self.params['result_count'] data_save = self.params['data_save'] if data_save.upper() == 'Y': self.getDataApi().WordPool.add_word_pool(corpus) config = { # 'useful_tags': ['Noun', 'Verb', 'Adjective', 'Determiner', 'Adverb', 'Conjunction', 'Josa', 'PreEomi', # 'Eomi', 'Suffix', 'Alpha', 'Number'], 'useful_tags': ['Noun', 'ProperNoun'], 'min_token_length': 5 } lexRank = LexRank(**config) lexRank.summarize(corpus) result_count = min(result_count, lexRank.num_sentences - 1) if result_count == 0: result_count = 1 result = lexRank.probe(result_count) return result
def _inner(*args, **kwargs): if not jpype.isJVMStarted(): jpype.startJVM(jvm_path) ret_val = func(*args, **kwargs) return ret_val
def __init__(self, connect, driver, user, password,isolation=''): config_parser = SafeConfigParser() config_parser.read(PATH_JDBC_CFG) # since it is impossible to call jpype.isThreadAttachedToJVM() before jpype.startJVM() # we won't check if JVM is started. classpath = config_parser.get("JVM", "classpath") javaHome = config_parser.get("JVM", "java_home") if (javaHome[0] != '/'): javaHome = "%s/%s" % (etl_util.get_app_root_path(), javaHome) args='-Djava.class.path=%s' % classpath try: os.environ['JAVA_HOME'] = javaHome jvm_path = jpype.getDefaultJVMPath() except TypeError: logging.error("failed to get default JVM path") raise if (jpype.isJVMStarted() == False): jpype.startJVM(jvm_path, args) logging.debug("Connecting with %s to %s with user %s" % (driver, connect, user)) self.conn = jaydebeapi.connect(driver, connect, user, password) #set isolation level if isolation!='': if 'mysql' in driver: sql='SET SESSION TRANSACTION ISOLATION LEVEL %s' % isolation elif 'teradata' in driver: sql='SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL %s' % isolation self._execute(sql)
def start_jvm(jvmargs=None): if jpype.isJVMStarted(): return if jvmargs is None: try: import psutil jvmsize = psutil.virtual_memory().available / 10**9 / 2 jvmargs = "-Xmx{}G".format(int(jvmsize)) except ImportError: jvmargs = "-Xmx4G" # must add dir and jarfile to support finding ixmp.properties module_root = os.path.dirname(__file__) jarfile = os.path.join(module_root, 'ixmp.jar') module_lib = os.path.join(module_root, 'lib') module_jars = [os.path.join(module_lib, f) for f in os.listdir(module_lib)] sep = ';' if os.name == 'nt' else ':' ix_classpath = sep.join([module_root, jarfile] + module_jars) jvm_args = ["-Djava.class.path=" + ix_classpath, jvmargs] jpype.startJVM(jpype.getDefaultJVMPath(), *jvm_args) # define auxiliary references to Java classes java.ixmp = java("at.ac.iiasa.ixmp") java.Integer = java("java.lang").Integer java.Double = java("java.lang").Double java.LinkedList = java("java.util").LinkedList java.HashMap = java("java.util").HashMap java.LinkedHashMap = java("java.util").LinkedHashMap
def _start_jvm(cls, jvm_path, jvm_options, driver_path, log4j_conf): if jvm_path is None: jvm_path = jpype.get_default_jvm_path() if driver_path is None: driver_path = os.path.join(cls._BASE_PATH, ATHENA_JAR) if log4j_conf is None: log4j_conf = os.path.join(cls._BASE_PATH, LOG4J_PROPERTIES) if not jpype.isJVMStarted(): _logger.debug('JVM path: %s', jvm_path) args = [ '-server', '-Djava.class.path={0}'.format(driver_path), '-Dlog4j.configuration=file:{0}'.format(log4j_conf) ] if jvm_options: args.extend(jvm_options) _logger.debug('JVM args: %s', args) if jpype.__version__.startswith("0.6"): jpype.startJVM(jvm_path, *args) else: jpype.startJVM(jvm_path, *args, ignoreUnrecognized=True, convertStrings=True) cls.class_loader = jpype.java.lang.Thread.currentThread().getContextClassLoader() if not jpype.isThreadAttachedToJVM(): jpype.attachThreadToJVM() if not cls.class_loader: cls.class_loader = jpype.java.lang.Thread.currentThread().getContextClassLoader() class_loader = jpype.java.net.URLClassLoader.newInstance( [jpype.java.net.URL('jar:file:{0}!/'.format(driver_path))], cls.class_loader) jpype.java.lang.Thread.currentThread().setContextClassLoader(class_loader)
def init_jvm(jvmpath=None, libraries=None, max_heap=1024): """Initializes the Java virtual machine (JVM). use Java in jpype.getDefaultJVMPath """ if jpype.isJVMStarted(): return None if not libraries: installpath = os.path.dirname(os.path.realpath(__file__)) libpaths = [ '{0}', '{0}{1}bin', '{0}{1}aho-corasick.jar', '{0}{1}shineware-common-1.0.jar', '{0}{1}shineware-ds-1.0.jar', '{0}{1}komoran-3.0.jar', '{0}{1}*' ] javadir = '%s%sjava' % (installpath, os.sep) args = [javadir, os.sep] libpaths = [p.format(*args) for p in libpaths] classpath = os.pathsep.join(libpaths) if jvmpath is None: jvmpath = jpype.getDefaultJVMPath() try: jpype.startJVM(jvmpath, '-Djava.class.path=%s' % classpath, '-Dfile.encoding=UTF8', '-ea', '-Xmx{}m'.format(max_heap)) except Exception as e: print(e)
def _get_connection(self, server, user, password, service, jdbc_driver, tags): try: # Check if the instantclient is available cx_Oracle.clientversion() except cx_Oracle.DatabaseError as e: # Fallback to JDBC use_oracle_client = False self.log.debug( 'Oracle instant client unavailable, falling back to JDBC: {}'. format(e)) connect_string = self.JDBC_CONNECT_STRING.format(server, service) else: use_oracle_client = True self.log.debug('Running cx_Oracle version {0}'.format( cx_Oracle.version)) connect_string = self.CX_CONNECT_STRING.format( user, password, server, service) try: if use_oracle_client: con = cx_Oracle.connect(connect_string) else: try: if jpype.isJVMStarted( ) and not jpype.isThreadAttachedToJVM(): jpype.attachThreadToJVM() jpype.java.lang.Thread.currentThread( ).setContextClassLoader( jpype.java.lang.ClassLoader.getSystemClassLoader()) con = jdb.connect(self.ORACLE_DRIVER_CLASS, connect_string, [user, password], jdbc_driver) except jpype.JException(jpype.java.lang.RuntimeException) as e: if "Class {} not found".format( self.ORACLE_DRIVER_CLASS) in str(e): msg = """Cannot run the Oracle check until either the Oracle instant client or the JDBC Driver is available. For the Oracle instant client, see: http://www.oracle.com/technetwork/database/features/instant-client/index.html You will also need to ensure the `LD_LIBRARY_PATH` is also updated so the libs are reachable. For the JDBC Driver, see: http://www.oracle.com/technetwork/database/application-development/jdbc/downloads/index.html You will also need to ensure the jar is either listed in your $CLASSPATH or in the yaml configuration file of the check. """ self.log.error(msg) raise self.log.debug("Connected to Oracle DB") self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=tags) except Exception as e: self.service_check(self.SERVICE_CHECK_NAME, AgentCheck.CRITICAL, tags=tags) self.log.error(e) raise return con
def start_JVM(): #only start JVM once since it is expensive if (not jpype.isJVMStarted()): #Use Brunel .jar files lib_ext = "-Djava.ext.dirs=" + lib_dir # headless execution of java is needed due to # calls to AWT. See git issue #70 headless = "-Djava.awt.headless=true" try: #First use explicit path if provided if brunel_util.JVM_PATH != "": jpype.startJVM(brunel_util.JVM_PATH, headless, lib_ext) else: # Try jpype's default way jpype.startJVM(jpype.getDefaultJVMPath(), headless, lib_ext) except: #jpype could not find JVM (this happens currently for IBM JDK) #Try to find the JVM starting from JAVA_HOME either as a .dll or a .so jvms = find_file('jvm.dll', os.environ['JAVA_HOME']) if (not jvms): jvms = find_file('libjvm.so', os.environ['JAVA_HOME']) if (not jvms): raise ValueError( "No JVM was found. First be sure the JAVA_HOME environment variable has been properly " "set before starting IPython. If it still fails, try to manually set the JVM using: " "brunel.brunel_util.JVM_PATH=[path]. Where 'path' is the location of the JVM file (not " "directory). Typically this is the full path to 'jvm.dll' on Windows or 'libjvm.so' on Unix " ) jpype.startJVM(jvms[0], headless, lib_ext)
def __init__(self, jvmpath=None): if not jpype.isJVMStarted(): jvm.init_jvm(jvmpath) kkmaJavaPackage = jpype.JPackage('kr.lucypark.kkma') KkmaInterfaceJavaClass = kkmaJavaPackage.KkmaInterface self.jki = KkmaInterfaceJavaClass() # Java instance
def getExternalConnection(self): try: # Fix import jpype if jpype.isJVMStarted() and not jpype.isThreadAttachedToJVM(): jpype.attachThreadToJVM() jpype.java.lang.Thread.currentThread().setContextClassLoader( jpype.java.lang.ClassLoader.getSystemClassLoader()) conn = jaydebeapi.connect("com.ibm.db2.jcc.DB2Driver", "jdbc:db2://" "{rechnername}.is.inf.uni-due.de:50{gruppennummer}/{database}".format( rechnername=rechnername, gruppennummer=re.match(r"([a-z]+)([0-9]+)", username, re.I).groups()[1], database=database #user=username.strip() ), { 'user': username, 'password': password, 'securityMechanism': "3" }, os.path.join(os.getcwd(), 'jdbc-1.0.jar') ) #conn.autocommit = False return conn except Exception as e: print(e)
def start_jvm(): if not jpype.isJVMStarted(): jpype.startJVM(jpype.get_default_jvm_path(), '-Dfile.encoding=UTF8', '-ea', '-Xmx1024m') if not jpype.isThreadAttachedToJVM(): jpype.attachThreadToJVM() global classpath_added if not classpath_added: add_classpaths([ f'{os.path.dirname(__file__)}/java/{lib}' for lib in [ 'poi-3.17.jar', 'poi-excelant-3.17.jar', 'poi-ooxml-3.17.jar', 'poi-ooxml-schemas-3.17.jar', 'poi-scratchpad-3.17.jar', 'lib/commons-codec-1.10.jar', 'lib/commons-collections4-4.1.jar', 'lib/commons-logging-1.2.jar', 'lib/log4j-1.2.17.jar', 'ooxml-lib/xmlbeans-2.6.0.jar', 'ooxml-lib/curvesapi-1.04.jar', ] ]) classpath_added = True
def _jdbc_connect_jpype(jclassname, jars, libs, *driver_args): import jpype if not jpype.isJVMStarted(): args = [] class_path = [] if jars: class_path.extend(jars) class_path.extend(_get_classpath()) if class_path: args.append('-Djava.class.path=%s' % os.path.pathsep.join(class_path)) if libs: # path to shared libraries libs_path = os.path.pathsep.join(libs) args.append('-Djava.library.path=%s' % libs_path) # jvm_path = ('/usr/lib/jvm/java-6-openjdk' # '/jre/lib/i386/client/libjvm.so') jvm_path = jpype.getDefaultJVMPath() jpype.startJVM(jvm_path, *args) if not jpype.isThreadAttachedToJVM(): jpype.attachThreadToJVM() if _jdbc_name_to_const is None: types = jpype.java.sql.Types types_map = {} for i in types.__javaclass__.getClassFields(): types_map[i.getName()] = i.getStaticAttribute() _init_types(types_map) global _java_array_byte if _java_array_byte is None: def _java_array_byte(data): return jpype.JArray(jpype.JByte, 1)(data) # register driver for DriverManager jpype.JClass(jclassname) return jpype.java.sql.DriverManager.getConnection(*driver_args)
def _jdbc_connect_jpype(jclassname, url, driver_args, jars, libs): import jpype if not jpype.isJVMStarted(): args = [] class_path = [] if jars: class_path.extend(jars) class_path.extend(_get_classpath()) if class_path: args.append('-Djava.class.path=%s' % os.path.pathsep.join(class_path)) if libs: # path to shared libraries libs_path = os.path.pathsep.join(libs) args.append('-Djava.library.path=%s' % libs_path) # jvm_path = ('/usr/lib/jvm/java-6-openjdk' # '/jre/lib/i386/client/libjvm.so') jvm_path = jpype.getDefaultJVMPath() global old_jpype if hasattr(jpype, '__version__'): try: ver_match = re.match('\d+\.\d+', jpype.__version__) if ver_match: jpype_ver = float(ver_match.group(0)) if jpype_ver < 0.7: old_jpype = True except ValueError: pass if old_jpype: jpype.startJVM(jvm_path, *args) else: jpype.startJVM(jvm_path, *args, ignoreUnrecognized=True, convertStrings=True) if not jpype.isThreadAttachedToJVM(): jpype.attachThreadToJVM() if _jdbc_name_to_const is None: types = jpype.java.sql.Types types_map = {} for i in types.__javaclass__.getClassFields(): if old_jpype: const = i.getStaticAttribute() else: const = i.__get__(i) types_map[i.getName()] = const _init_types(types_map) global _java_array_byte if _java_array_byte is None: def _java_array_byte(data): return jpype.JArray(jpype.JByte, 1)(data) # register driver for DriverManager jpype.JClass(jclassname) if isinstance(driver_args, dict): Properties = jpype.java.util.Properties info = Properties() for k, v in driver_args.items(): info.setProperty(k, v) dargs = [ info ] else: dargs = driver_args return jpype.java.sql.DriverManager.getConnection(url, *dargs)
def coverage(request): yield if request.config.getoption("--jacoco") and jpype.isJVMStarted(): # Force dump of the jacoco agent RT = jpype.JClass("org.jacoco.agent.rt.RT") agent = RT.getAgent() agent.dump(False)
def import_classes(): if not isJVMStarted(): print "Jvm has not been started! No class is imported!" return global Node,BoundaryUtilsTestUtils,QuadraturePoint,TimoshenkoExactBeam2D,CommonPostProcessor global CommonUtils,WeakformProcessorMonitors,Coordinate,WeakformProcessor2DDemoUtils global QuadPixcellAtomSample,QuadPixcellRectangleSample,QuadPixcellManager global LocationAdaptiveFilter,Triangle,Quadrangle,UniformTensionInfinitePlateSample global SampleLSF,SampleC1LSF,SampleC2LSF Node=JPackage('net').epsilony.utils.geom.Node BoundaryUtilsTestUtils=JPackage('net').epsilony.simpmeshfree.model.test.BoundaryUtilsTestUtils QuadraturePoint=JClass('net.epsilony.simpmeshfree.utils.QuadraturePoint') TimoshenkoExactBeam2D=JClass('net.epsilony.simpmeshfree.model2d.TimoshenkoExactBeam2D') CommonPostProcessor=JClass('net.epsilony.simpmeshfree.model.CommonPostProcessor') CommonUtils=JPackage('net').epsilony.spfun.CommonUtils WeakformProcessorMonitors=JPackage('net').epsilony.simpmeshfree.model.WeakformProcessorMonitors Coordinate = JPackage('net').epsilony.utils.geom.Coordinate WeakformProcessor2DDemoUtils=JClass('net.epsilony.simpmeshfree.model2d.test.WeakformProcessor2DDemoUtils') QuadPixcellAtomSample=JPackage('net').epsilony.simpmeshfree.adpt2d.sample.QuadPixcellAtomSample QuadPixcellRectangleSample=JPackage('net').epsilony.simpmeshfree.adpt2d.sample.QuadPixcellRectangleSample QuadPixcellManager=JPackage('net').epsilony.simpmeshfree.adpt2d.QuadPixcellManager LocationAdaptiveFilter=JPackage('net').epsilony.simpmeshfree.adpt2d.sample.LocationAdaptiveFilter Triangle = JClass('net.epsilony.utils.geom.Triangle') Quadrangle = JClass('net.epsilony.utils.geom.Quadrangle') UniformTensionInfinitePlateSample=JPackage('net').epsilony.simpmeshfree.model2d.test.UniformTensionInfinitePlateSample SampleLSF=JClass('net.epsilony.levelset.functions.SampleLSF') SampleC1LSF=JClass('net.epsilony.levelset.functions.SampleC1LSF') SampleC2LSF=JClass('net.epsilony.levelset.functions.SampleC2LSF')
def nlp_twitter(text): import jpype if jpype.isJVMStarted(): jpype.attachThreadToJVM() from konlpy.tag import Twitter return [n[0] for n in Twitter().pos(text, norm=False, stem=True) if n[1] in ('Noun', 'ProperNoun')]
def init_jpype(jarloc=None, jvmpath=None): if not HAVE_JPYPE: print( "Cannot initialize jpype because it couldn't be imported. Make sure jpype is installed" ) return jarloc = jarloc or config.__dict__.get( 'JARLOC', '%s/infodynamics/infodynamics.jar' % os.path.dirname(os.__file__)) assert os.path.exists(jarloc), "Jar file %s doesn't exist" % jarloc jvmpath = jvmpath or config.__dict__.get('JVMPATH', getDefaultJVMPath()) logger.debug( "measures_infth.init_jpype: Setting jpype jvmpath = {0}".format( jvmpath)) logger.debug( "measures_infth.init_jpype: setting JIDT jar location = {0}".format( jarloc)) # startJVM(getDefaultJVMPath(), "-ea", "-Xmx2048M", "-Djava.class.path=" + jarLocation) if not isJVMStarted(): logger.debug("Starting JVM") startJVM(jvmpath, "-ea", "-Xmx8192M", "-Djava.class.path=" + jarloc) else: logger.debug("Attaching JVM") attachThreadToJVM()
def start_JVM(): #only start JVM once since it is expensive if (not jpype.isJVMStarted()): #Use Brunel .jar files lib_ext = "-Djava.ext.dirs=" + lib_dir # headless execution of java is needed due to # calls to AWT. See git issue #70 headless = "-Djava.awt.headless=true" try: #First use explicit path if provided if brunel_util.JVM_PATH != "": jpype.startJVM(brunel_util.JVM_PATH, headless, lib_ext) else: # Try jpype's default way jpype.startJVM(jpype.getDefaultJVMPath(), headless, lib_ext) except: #jpype could not find JVM (this happens currently for IBM JDK) #Try to find the JVM starting from JAVA_HOME either as a .dll or a .so jvms = find_file('jvm.dll', os.environ['JAVA_HOME']) if (not jvms): jvms = find_file('libjvm.so', os.environ['JAVA_HOME']) if (not jvms): raise ValueError( "No JVM was found. First be sure the JAVA_HOME environment variable has been properly " "set before starting IPython. If it still fails, try to manually set the JVM using: " "brunel.brunel_util.JVM_PATH=[path]. Where 'path' is the location of the JVM file (not " "directory). Typically this is the full path to 'jvm.dll' on Windows or 'libjvm.so' on Unix ") jpype.startJVM(jvms[0], headless, lib_ext)
def jvm(max_mem=None): """Return the jpype module, after guaranteeing the JVM is running and the classpath has been loaded properly.""" jarpaths = (os.path.abspath( os.path.join(os.path.dirname(__file__), "../lib")), os.path.abspath( os.path.join(os.path.dirname(__file__), "../dist"))) log4j_properties_path = os.path.abspath( os.path.join(os.path.dirname(__file__), "../log4j.properties")) if not jpype.isJVMStarted(): max_mem = get_jvm_max_mem(max_mem) LOG.debug("Default JVM path is %s" % jpype.getDefaultJVMPath()) jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s:%s" % jarpaths, # "-Dlog4j.debug", # turn on log4j internal debugging "-Dlog4j.configuration=file://%s" % log4j_properties_path, "-Xmx%sM" % max_mem) # override the log level set in log4j configuration file this can't be # done on the JVM command line (i.e. -Dlog4j.rootLogger= is not # supported by log4j) _set_java_log_level(FLAGS.debug.upper()) if FLAGS.capture_java_debug: _setup_java_capture(sys.stdout, sys.stderr) return jpype
def fault_poly_from_mls(fault_source_geom, dip, upp_seis_depth, low_seis_depth): """Given a fault source geometry (as a MultiLineString), dip, upper seismogenic depth, lower seismogenic depth, and grid spacing (in km), create a 3D polygon to represent the fault. :param fault_source_geom: :class:`django.contrib.gis.geos.collections.MultiLineString` :param float dip: Angle of dip, from 0.0 to 90.0 degrees (inclusive) :param float upp_seis_depth: Upper seismogenic depth :param float low_seis_depth: Lower seismogenic depth :returns: 3D polygon representing the complete fault geometry :rtype: :class:`django.contrib.gis.geos.collections.Polygon` """ # I take no responsibility for writing this #: Value is in kilometers GRID_SPACING = 1.0 if not jpype.isJVMStarted(): # start jvm once jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s" % settings.GEOCLUDGE_JAR_PATH) FT = jpype.JClass('org.opensha.sha.faultSurface.FaultTrace') LOC = jpype.JClass('org.opensha.commons.geo.Location') LOC_LIST = jpype.JClass('org.opensha.commons.geo.LocationList') SGS = jpype.JClass('org.opensha.sha.faultSurface.StirlingGriddedSurface') coords = fault_source_geom.coords fault_trace = FT('') for line_str in coords: for lon, lat in line_str: # warning: the ordering of lat/lon is switched here # be careful loc = LOC(lat, lon) fault_trace.add(loc) surface = SGS(fault_trace, float(dip), float(upp_seis_depth), float(low_seis_depth), GRID_SPACING) # now we make a polygon with the perimeter coords: poly_coords = [] for per_loc in surface.getSurfacePerimeterLocsList(): lon = per_loc.getLongitude() lat = per_loc.getLatitude() depth = per_loc.getDepth() poly_coords.append((lon, lat, depth)) return Polygon(poly_coords)
def __init__(self, gui=False, thd=False): ''' Create a link with netlogo. Underneath, the netlogo jvm is started through jpype. :param gui: boolean, if true run netlogo with gui, otherwise run in headless mode. Defaults to false. :param thd: boolean, if thrue start netlogo in 3d mode. Defaults to false ''' if not jpype.isJVMStarted(): # netlogo jars jars = [NETLOGO_HOME + r'/lib/scala-library.jar', NETLOGO_HOME + r'/lib/asm-all-3.3.1.jar', NETLOGO_HOME + r'/lib/picocontainer-2.13.6.jar', NETLOGO_HOME + r'/lib/log4j-1.2.16.jar', NETLOGO_HOME + r'/lib/jmf-2.1.1e.jar', NETLOGO_HOME + r'/lib/pegdown-1.1.0.jar', NETLOGO_HOME + r'/lib/parboiled-core-1.0.2.jar', NETLOGO_HOME + r'/lib/parboiled-java-1.0.2.jar', NETLOGO_HOME + r'/lib/mrjadapter-1.2.jar', NETLOGO_HOME + r'/lib/jhotdraw-6.0b1.jar', NETLOGO_HOME + r'/lib/quaqua-7.3.4.jar', NETLOGO_HOME + r'/lib/swing-layout-7.3.4.jar', NETLOGO_HOME + r'/lib/jogl-1.1.1.jar', NETLOGO_HOME + r'/lib/gluegen-rt-1.1.1.jar', NETLOGO_HOME + r'/NetLogo.jar', PYNETLOGO_HOME + r'/external_files/netlogoLink.jar'] # format jars in right format for starting java virtual machine # TODO the use of the jre here is only relevant under windows # apparently # might be solvable by setting netlogo home user.dir joined_jars = jar_separator.join(jars) jarpath = '-Djava.class.path={}'.format(joined_jars) jvm_handle = jpype.getDefaultJVMPath() jpype.startJVM(jvm_handle, jarpath, "-Xms128M","-Xmx1024m") jpype.java.lang.System.setProperty('user.dir', NETLOGO_HOME) if sys.platform=='darwin': jpype.java.lang.System.setProperty("java.awt.headless", "true"); debug("jvm started") link = jpype.JClass('netlogoLink.NetLogoLink') debug('NetLogoLink class found') if sys.platform == 'darwin' and gui: info('on mac only headless mode is supported') gui=False self.link = link(gui, thd) debug('NetLogoLink class instantiated')
def make_java_task(num_calcs): if not jpype.isJVMStarted(): jpype.startJVM(jpype.getDefaultJVMPath(), "-d log4j.debug") res = jpype.JInt(num_calcs) jpype.shutdownJVM() return res
def get_sign_rsa(input_string): if not jpype.isJVMStarted(): jpype.startJVM(jvmPath, jvmArg) RSAUtil = jpype.JClass('com.common.RSAUtil') rsa = RSAUtil() rsa.setPrivateKey(private_key); sign = rsa.sign(input_string) return sign
def __init__(self, jvmpath=None): if not jpype.isJVMStarted(): jvm.init_jvm(jvmpath) jhannanumJavaPackage = jpype.JPackage('kr.lucypark.jhannanum.comm') HannanumInterfaceJavaClass = jhannanumJavaPackage.HannanumInterface self.jhi = HannanumInterfaceJavaClass() # Java instance self.tagset = utils.read_json('%s/data/tagset/hannanum.json' % utils.installpath)
def _open(self): logger.info("Starting JVM") if isJVMStarted(): logger.warn("JVM is already started. Only one connection can be opened," "previously created connection will be used.") return startJVM(getDefaultJVMPath(), *self._jvm_args())
def __init__(self, jvmpath=None): if not jpype.isJVMStarted(): jvm.init_jvm(jvmpath) tktJavaPackage = jpype.JPackage('kr.lucypark.tkt') TktInterfaceJavaClass = tktJavaPackage.TktInterface self.jki = TktInterfaceJavaClass() self.tagset = utils.read_json('%s/data/tagset/twitter.json' % utils.installpath)
def _init_jvm(): if not jpype.isJVMStarted(): jars = [] for top, dirs, files in os.walk(imp.find_module("twkorean")[1] + "/data/lib"): for nm in files: jars.append(os.path.join(top, nm)) jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=%s" % os.pathsep.join(jars))
def runTest() : runner = unittest.TextTestRunner() result = runner.run(suite()) if jpype.isJVMStarted(): jpype.shutdownJVM() if not result.wasSuccessful(): sys.exit(1)
def __init__(self, jvmpath=None): if not jpype.isJVMStarted(): jvm.init_jvm(jvmpath) kkmaJavaPackage = jpype.JPackage('kr.lucypark.kkma') KkmaInterfaceJavaClass = kkmaJavaPackage.KkmaInterface self.jki = KkmaInterfaceJavaClass() # Java instance self.tagset = utils.read_json('%s/data/tagset/kkma.json' % utils.installpath)
def __init__(self, jvmpath=None, max_heap_size=1024): if not jpype.isJVMStarted(): jvm.init_jvm(jvmpath, max_heap_size) oktJavaPackage = jpype.JPackage('kr.lucypark.okt') OktInterfaceJavaClass = oktJavaPackage.OktInterface self.jki = OktInterfaceJavaClass() self.tagset = utils.read_json('%s/data/tagset/twitter.json' % utils.installpath)
def setup_module(module): # Module-level setup. if not jpype.isJVMStarted(): jvm_args = [ '-Xmx%dM' % (MAX_JVM_HEAP_SIZE_BYTES // 1000 ** 2), ] jpype.startJVM(jpype.getDefaultJVMPath(), *jvm_args) module.JavaDoubleArray = jpype.JArray(jpype.JDouble, 1)
def __init__(self): jvmPath = jpype.getDefaultJVMPath() #the path of jvm.dll classpath = "E:\\stu-support\\EmotionClassify\\bin" #the path of PasswordCipher.class jvmArg = "-Djava.class.path=" + classpath if not jpype.isJVMStarted(): #test whether the JVM is started jpype.startJVM(jvmPath,jvmArg) #start JVM javaClass = jpype.JClass("sa.EmotionClassify") #create the Java class self.Emo = javaClass()
def katechToWgs84(x, y): if(type(x) != type(0.0) or type(y) != type(0.0)): print "Parameters must be type of float" return -1 """ classpath = os.path.join(os.path.abspath('../'), 'bin') jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=%s" % classpath) """ # jarpath = os.path.join(os.path.abspath('.'), 'GeoTrans.jar') # jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.ext.dirs=%s" % jarpath) # JVM Start if not jpype.isJVMStarted(): jarpath = ".:GeoTrans.jar" jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=%s" % (jarpath)) # Package Import GeoPkg = jpype.JPackage("GeoTrans") # Class Import GeoPoint = GeoPkg.GeoPoint GeoTrans = GeoPkg.GeoTrans # Point convert oKA = GeoPoint(x, y) oGeo = GeoTrans.convert(GeoTrans.KATEC, GeoTrans.GEO, oKA) return {"x":oGeo.getX(), "y":oGeo.getY()}