티스토리 뷰

Programing/Python

PyJNIus 사용기

Tomining 2016. 7. 28. 21:28
들어가며...

Python 으로 개발을 하다보면 이미 만들어진 Java Class 를 활용해야 할 때가 있습니다. 기본적으로 Python 은 interpreter 언어로 interpreter 만 있으면 되지만 Java 의 경우는 JVM 환경이 있어야 수행이 가능합니다. 따라서 간단히 진행하기가 어렵습니다.


Java Class 사용을 위한 Python Packages

Java Class 를 사용하기 위해 어떤 것들이 있는지 알아보겠습니다.
  • Py4j - 내부적으로 Java Server 를 구동해서 연동하는 방식
  • JPype - JVM 을 startup/shutdown 을 Python 에서 관리
  • JCC - 2.X 버전만 지원
  • PyJNIus - JVM 실행 파일로 실행

JCC의 경우는 2.X 버전만 지원하기에 고려사항에서 제외하였습니다.
Py4j와 JPype 은 실행방식이 유사하며, JVM 을 python 에서 startup/shutdown 해 주어야 합니다. 만약 startup 상태에서 startup 을 다시하거나 하면 python interpreter 도 동작하지 않는 문제점이 있었습니다.

JPype 사용기는 링크를 참고 바랍니다.


사전준비

PyJNIus 설치를 시작하기 전에 먼저 설치되어 있어야 할 것들이 있습니다.


PyJNIus 설치

[환경]
  • window 7(64bit)
  • python 3.5.1

설치를 시작하기 

Python 에서 Java Class 를 접근을 도와주는 패키지로 아래 github 에서 코드를 받을 수 있습니다.

python setup.py install

WARNING: Not able to assign machine() = AMD64 to a cpu value!

         Using cpu = 'i386' instead!

C:\dev\dev_env\virtualenv\django_uit\lib\site-packages\setuptools\dist.py:285: UserWarning: Normalizing '1.1-dev' to '1.1.dev0'

  normalized_version,

running install

running bdist_egg

running egg_info

writing jnius.egg-info\PKG-INFO

writing requirements to jnius.egg-info\requires.txt

writing top-level names to jnius.egg-info\top_level.txt

writing dependency_links to jnius.egg-info\dependency_links.txt

reading manifest file 'jnius.egg-info\SOURCES.txt'

reading manifest template 'MANIFEST.in'

warning: no files found matching '*COPYING'

writing manifest file 'jnius.egg-info\SOURCES.txt'

installing library code to build\bdist.win-amd64\egg

running install_lib

running build_py

running build_ext

skipping 'jnius\jnius.c' Cython extension (up-to-date)

creating build\bdist.win-amd64\egg

creating build\bdist.win-amd64\egg\jnius

copying build\lib.win-amd64-3.5\jnius\jnius.cp35-win_amd64.pyd -> build\bdist.win-amd64\egg\jnius

copying build\lib.win-amd64-3.5\jnius\reflect.py -> build\bdist.win-amd64\egg\jnius

copying build\lib.win-amd64-3.5\jnius\signatures.py -> build\bdist.win-amd64\egg\jnius

copying build\lib.win-amd64-3.5\jnius\__init__.py -> build\bdist.win-amd64\egg\jnius

copying build\lib.win-amd64-3.5\jnius_config.py -> build\bdist.win-amd64\egg

byte-compiling build\bdist.win-amd64\egg\jnius\reflect.py to reflect.cpython-35.pyc

byte-compiling build\bdist.win-amd64\egg\jnius\signatures.py to signatures.cpython-35.pyc

byte-compiling build\bdist.win-amd64\egg\jnius\__init__.py to __init__.cpython-35.pyc

byte-compiling build\bdist.win-amd64\egg\jnius_config.py to jnius_config.cpython-35.pyc

creating stub loader for jnius\jnius.cp35-win_amd64.pyd

byte-compiling build\bdist.win-amd64\egg\jnius\jnius.py to jnius.cpython-35.pyc

creating build\bdist.win-amd64\egg\EGG-INFO

copying jnius.egg-info\PKG-INFO -> build\bdist.win-amd64\egg\EGG-INFO

copying jnius.egg-info\SOURCES.txt -> build\bdist.win-amd64\egg\EGG-INFO

copying jnius.egg-info\dependency_links.txt -> build\bdist.win-amd64\egg\EGG-INFO

copying jnius.egg-info\requires.txt -> build\bdist.win-amd64\egg\EGG-INFO

copying jnius.egg-info\top_level.txt -> build\bdist.win-amd64\egg\EGG-INFO

writing build\bdist.win-amd64\egg\EGG-INFO\native_libs.txt

zip_safe flag not set; analyzing archive contents...

jnius.__pycache__.jnius.cpython-35: module references __file__

creating 'dist\jnius-1.1.dev0-py3.5-win-amd64.egg' and adding 'build\bdist.win-amd64\egg' to it

removing 'build\bdist.win-amd64\egg' (and everything under it)

Processing jnius-1.1.dev0-py3.5-win-amd64.egg

removing 'c:\dev\dev_env\virtualenv\django_uit\lib\site-packages\jnius-1.1.dev0-py3.5-win-amd64.egg' (and everything under it)

creating c:\dev\dev_env\virtualenv\django_uit\lib\site-packages\jnius-1.1.dev0-py3.5-win-amd64.egg

Extracting jnius-1.1.dev0-py3.5-win-amd64.egg to c:\dev\dev_env\virtualenv\django_uit\lib\site-packages

jnius 1.1.dev0 is already the active version in easy-install.pth

 

Installed c:\dev\dev_env\virtualenv\django_uit\lib\site-packages\jnius-1.1.dev0-py3.5-win-amd64.egg

Processing dependencies for jnius==1.1.dev0

Searching for Cython==0.24.1

Best match: Cython 0.24.1

Adding Cython 0.24.1 to easy-install.pth file

Installing cygdb-script.py script to C:\dev\dev_env\virtualenv\django_uit\Scripts

Installing cygdb.exe script to C:\dev\dev_env\virtualenv\django_uit\Scripts

Installing cythonize-script.py script to C:\dev\dev_env\virtualenv\django_uit\Scripts

Installing cythonize.exe script to C:\dev\dev_env\virtualenv\django_uit\Scripts

Installing cython-script.py script to C:\dev\dev_env\virtualenv\django_uit\Scripts

Installing cython.exe script to C:\dev\dev_env\virtualenv\django_uit\Scripts

 

Using c:\dev\dev_env\virtualenv\django_uit\lib\site-packages

Searching for six==1.10.0

Best match: six 1.10.0

Adding six 1.10.0 to easy-install.pth file

 

Using c:\dev\dev_env\virtualenv\django_uit\lib\site-packages


Finished processing dependencies for jnius==1.1.dev0

설치 후 정상적으로 되는지는 아래와 같이 확인 할 수 있습니다.

from jnius import autoclass

autoclass(‘java.lang.System’).out.println(‘hello world’)


PyJNIus 사용하기



import jnius_config
jnius_config.add_options(‘-Xrs’, ‘-Xmx4096’)
jnius_config.set_classpath(‘.’, ‘{classpath 경로}’)

import jnius

위 함수로 옵션을 설정할 경우 CLASSPATH 환경변수를 override 하게 됩니다.


from jnius import JavaClass, MetaJavaClass

class Stack(JavaClass):
    __javaclass__ = ‘java/util/Stack'
    __metaclass__ = MetaJavaClass



from jnius import autoclass

autoclass(‘java.lang.System’).out.println(‘hello world’)


느낀점...

jar 같은 이미 컴파일된 java class들을 호출하여 사용하는 방식은 autoclass 를 통해 손쉽게 호출할 수 있었습니다.


참고


'Programing > Python' 카테고리의 다른 글

Python + Jenkins 연동  (0) 2016.08.26
sqlparse 사용기  (0) 2016.08.18
PEP8 이란?  (0) 2016.07.27
APScheduler 사용기  (0) 2016.07.26
JPype 사용기  (0) 2016.07.19
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함