Accessing JVM from python

一个人想着一个人 提交于 2019-11-27 17:45:52

问题


>>> import boilerpipe
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Anaconda\lib\site-packages\boilerpipe\__init__.py", line 10, in <module>
    jpype.startJVM(jpype.getDefaultJVMPath(), "-Djava.class.path=%s" % os.pathsep.join(jars))
  File "C:\Anaconda\lib\site-packages\jpype\_core.py", line 50, in startJVM
    _jpype.startup(jvm, tuple(args), True)
RuntimeError: Unable to load DLL [C:\Program Files\Java\jre7\bin\client\jvm.dll], error = The specified module could not be found.
 at native\common\include\jp_platform_win32.h:58

Tried: Reinstalling jvm

>> import ctypes
>> import os
>> os.chdir(r"<path to Java bin client folder>")
>> ctypes.CDLL("jvm.dll")
Still unable to fix

Edit: Tried code below, still stuck:

from py4j.java_gateway import JavaGateway gateway = JavaGateway() It gives the same error as before.


回答1:


Check Please !

  1. Give a true path ("C:\\Program Files\\Java\\jre7\\bin\client\\jvm.dll")
  2. Check all 32 or 64 bit (my offer always use 32bit Anaconda,JRE7,Python etc.)
  3. Install pywin32 (of course python2.7.9)
  4. Give permission to jvm.dll(can run)
  5. After all is ok, try: import ctypes ctypes.CDLL('C:\\Program Files\\Java\\jre7\\bin\\client\\jvm.dll')

***Maybe can't throw the hook of some java versions i used jre-7u55-windows-i586.exe

i hope helpful ! Best regards !

Works on wine(no bug):




回答2:


Answering because I don't have enough rep for commenting; Try using raw strings instead of the normal ones allowing "\x" escapes. Try:

>>> ctypes.CDLL(r'C:\Program Files (x86)\Java\jre1.8.0_40\bin\client\jvm.dll')

Because the "\..." parts of the string could very well be escapes.




回答3:


The answers above are not sufficient, one also needs to install the Microsoft Visual C++ 2010 Redistributable Package (x86) as per Bjorns answer.

After installed the C++ redistributable:

  1. Set JAVA_HOME environment variable to ("C:\Program Files\Java\jre7")
  2. Use paths as mentioned above to start jvm:

    path_to_jvm = "C:\Program Files\Java\jre7\bin\client\jvm.dll"

    jpype.startJVM(path_to_jvm)

    jpype.startJVM(jpype.getDefaultJVMPath())




回答4:


If you are trying to access JVM on Windows operation system, make sure you have the right version of JVM (32-bit or 64-bit) installed. In my case python was 64-bit and once I installed Java 64-bit, it worked without needing to specify the path or any other extra requirements.



来源:https://stackoverflow.com/questions/32045648/accessing-jvm-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!