16
>>> 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.

Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142
  • 1
    Loading the JVM from a DLL is very hard even from C. I suggest you load the JVM using `java` from the command line or use Jython which runs in the JVM already. – Peter Lawrey Aug 17 '15 at 08:20
  • @PeterLawrey Thanks for comment! I am unaware of `Jython`. Should I just install it and run my python script again? Or there is more to it? – Abhishek Bhatia Aug 17 '15 at 08:40
  • Jython runs Python scripts inside the JVM. I assume there is more to it but this is designed and supported to work with the JVM it is running in. – Peter Lawrey Aug 17 '15 at 08:53
  • https://stackoverflow.com/questions/3652554/calling-java-from-python – ρss Aug 17 '15 at 09:48
  • @ρss I am very new using java in python. How can this solve my issue? I just want to simply use this package. – Abhishek Bhatia Aug 17 '15 at 11:00
  • @ρss Tried this. `>>> from py4j.java_gateway import JavaGateway >>> gateway = JavaGateway() ` Still the same error. – Abhishek Bhatia Aug 17 '15 at 13:03
  • @AbhishekBhatia Please update your question with the new tests that you have made, instead of mentioning them in a comment. – ρss Aug 17 '15 at 14:59
  • 1
    @ρss Okay done, please check and help if possible. Thanks. – Abhishek Bhatia Aug 20 '15 at 11:38
  • Not an answer, but I would like to also recommend trying Jython. From Jython you can access everything in Java. What Jython is, is that you run Python *inside* Java and so direct access to everything inside Java. – Prof. Falken Aug 20 '15 at 13:04
  • Also you don't say what the problem is with the last example. – Prof. Falken Aug 20 '15 at 13:18
  • @Prof.Falken I don't want to install another python distribution. Is it possible to install `Jython` to already present python version. If that makes some sense. – Abhishek Bhatia Aug 21 '15 at 07:08
  • @AbhishekBhatia, no, not exactly. You install it to already present *Java* installation. Or you just include the Jython **jar** file inside your Java project. Then nothing at all needs to be installed. http://www.jython.org/downloads.html – Prof. Falken Aug 21 '15 at 11:58
  • @Prof.Falken Sorry I don't exactly understand. Please explain `Jython jar file inside your Java project`. Are you talking about the boilerpipe library here or my script which uses boilerpipe. – Abhishek Bhatia Aug 21 '15 at 14:03
  • @AbhishekBhatia, I am saying that you if you had Jython, you could import any Java library. – Prof. Falken Aug 21 '15 at 15:32
  • The problem with jython is that a lot of python stuff doesn't work with it (a lot of the more advanced modules out there use C extensions). I've had good initial success with [jpype](https://github.com/originell/jpype), though I haven't used it super extensively. – yshavit Aug 22 '15 at 06:19

4 Answers4

3

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): enter image description here

dsgdfg
  • 1,492
  • 11
  • 18
  • 3
    Thanks! I get this error unforunately. `>>> ctypes.CDLL('C:\Program Files (x86)\Java\jre1.8.0_40\bin\client\jvm.dll') Traceback (most recent call last): File "", line 1, in File "C:\Anaconda\lib\ctypes\__init__.py", line 365, in __init__ self._handle = _dlopen(self._name, mode) WindowsError: [Error 126] The specified module could not be found` – Abhishek Bhatia Aug 22 '15 at 17:34
  • 1
    How does one give permission to the dll to "can run"? We are searching the web but the keywords are so generic we are not finding good answers. – Praxiteles Jul 19 '18 at 10:11
  • Depends on the operating system version. Some application extensions only require access (sometimes the right to access and/or user and/or user group and/or access class and/or service point etc.). – dsgdfg Jul 20 '18 at 09:04
3

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.

knight
  • 435
  • 5
  • 18
1

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())

frage
  • 719
  • 7
  • 15
1

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.

Hamid Heydarian
  • 802
  • 9
  • 16