Reputation: 469
I have a java class file which I would like to use from within my Python framework. I know of jython, but I don't want to run my whole Python code with jython, instead I would prefer to import to somehow precompile it to a python usable class, is something like this possible?
Upvotes: 0
Views: 224
Reputation: 110301
As already put in the comments: Any java code will need a complete JVM to run.
That said, googling for your problem, there seems to be a project that starts a JVM from within the Python process and allows you to interact with it: http://pythonhosted.org/javabridge/
Despite little information on the project page, it did install on my second try in a fresh Python 3.6 environment (I just needed to get this outside of the straightforward installs in a Fedora system).
Even if you get that working, you probably should consider rewriting your Java code to Python, by hand, if it is a single class you will need in a system mostly powered by NumPy and SciPy.
If this Javabridge project fails, the alternative is to expose your Java class in some RPC server, possibly using XMLRPC, or just as a less featured webservice, and call into it from Python by using a networking protocol. Since you get to here, there are plenty of stuff you could do, as using redis or rabbitmq as brokers to pass messages to and from your Java code, and so on - what all these approaches have in common is that you have to get you Java code running in its own process in the JVM, and in a server loop that can take the remote calls.
Upvotes: 1