How to use different languages in one project

1.6k Views Asked by At

Recently I decided to write a program in Java and Python but I have no idea how to do this... Is there any way to make the classes communicate together in the project so that for example Python methods could be called by Java? Is there any framework which makes it possible for me to handle this? I also wondered how experienced programmers write their projects with multiple languages.

My Idea was to write the Gui of my program with JavaFX because of its awesome look and a speech recognition in Python using google library for this.

1

There are 1 best solutions below

4
On BEST ANSWER

There are 3rd party libraries that can be used for this, but that bridge could also be made without if you are willing to include a third language as well. Due to the title of your question, this part of the answer provides the more generic "different languages" answer.

The Java JNI (Java Native Interface) allows you to bridge between Java and native code. Technically, that native code could be for any language that compiles to native code or in some way supports it, but in practice JNI is often used to make Java work with either C or C++.

There are a lot of examples for Java/C and Java/C++. Python also supports working alongside native code as well. See Calling C/C++ from Python? So what you could do is to use C++ as a middleman between Java and Python. You would need to learn to use a Python/native bridge, and learn to use a Java/native bridge, then put the two together to go Python/native/Java.

This has pros and cons. Con: You now are forced to introduce a third language and to support a larger complexity. Pro: If you are open to using multiple languages together, this naturally opens the door to using many different languages together, since lots of languages support calling native code.

Another option

Another way to do this is to have each different language run in a separate process and to have the different processes pass their data back and forth by some type of inter-process communication. Some common ways to do this are through sockets (the same things used for network communication), and by files on disk, and by shared memory.

Though sockets are probably best known for network communications (ie: "http internet traffic generally opens a socket to the server on port 80"), it is also commonly used for local inter-process communication as well. You would open a socket the same way, but it would generally be to host name "localhost", which is usually IP address 127.0.0.1.

This has the added complexity of keeping track of messages between processes and actually processing them. Depending on your environment and needs, this complexity may be more or less complicated than running multiple languages in 1 process. If you need to scale your application later, having it separated into multiple processes might make it easier to distribute the load between multiple machines in the case of sockets or files.


For your specific situation

You mentioned that you want Java for the GUI. Java GUI is handled on its own separate thread anyway, so you should be able to call a GUI initialization function in Java from Python that sets up and displays your GUI. Just be aware of the concurrency issues (that is, the multi-threading issues).

Also, I had forgotten about Jython. Thanks to @Jeremy for pointing that out. I have never used it personally, but it is "Python for the Java platform." You could look into that to see if it provides what you need.

Also, according to the comment by @that other guy:

The Google Cloud Speech API has a Java API just like it has a Python API. Just use that and save yourself a ton of trouble.

So you might not even need to do any bridging in the first place. However, I leave my answer to your question as you asked it in case you decide you want to allow for the multi-language support anyway, and for future users.