I am creating a Windows program that so far has a .bat file calling a .pyw file, and I need functions from Java and C++. How can I do this?(I don't mind creating a new batch or python file, and I already have the header file for the C++ section and a .jar file for my java components. (For Java I use Eclipse Java Mars, and it's Java 8u101)) Thanks!!!
-
If that makes sense to you: you can have a look into www.jython.org - maybe it is an option for you to run python code within your JVM. – GhostCat Aug 16 '16 at 07:03
2 Answers
This is rather simple for C++: you have to compile a library with the function, import it in Python and... call it! Python has a powerful ctypes module in the standard library to handle this kind of tasks.
Here is an example of loading print()
function from hypothetical libc.dll
.
from ctypes import *
libc = cdll.LoadLibrary("libc.dll")
>>> print(libc.time(None))
1150640792
Calling Java from Python is covered here: How to call a java function from python/numpy?

- 22,280
- 12
- 56
- 83
-
1[CFFI](https://cffi.readthedocs.io/en/latest/) is another module worth mentioning when discussing calling C from Python. – Angew is no longer proud of SO Aug 16 '16 at 06:49
-
Thanks, @Angew, I was totally unaware about CFFI and always relied on pure ctypes. Definitely worth checking! – Zaur Nasibov Aug 16 '16 at 06:54
You can load C++ function and execute it from Python like BasicWolf explained in his answer. For Java, Jython might be a good approach. But then there's a problem - you will need to be dependent on Jython which is not up to date with the latest versions of Python. You will face compatibility issues with different libraries too.
I would recommend compiling your C++ and Java functions to create individual binaries out of them. Then execute these binaries from within Python, passing the arguments as command line parameters. This way you can keep using CPython. You can interoperate with programs written in any language.

- 11,635
- 4
- 39
- 50
-
So could I just compile all the C++ commands into one DLL, all the Java commands into another DLL, and call both from the Python script the way @BasicWolf said? – Pranav Sharma Sep 04 '16 at 23:44