Dean Christian Armada Dean Christian Armada - 1 year ago 89
Python Question

How to integrate Java and Python?

I am trying something out combining java and python for some encryption reasons where the encryption code is actually a java file from the provider but I am writing a python code. So what I want is to execute the encryption code on java using

x = os.system("java")

and just return the value from the java execution like in a function

print x

Like this:

import os

x = os.system("java HelloWorld")
print x # This should print HELLO WORLD

public class HelloWorld {
public static void main(String[] args) {
String x;
x = "Hello World";
return "HELLO WORLD";


The java file above by the way returns an error: error: incompatible types: unexpected return value
return x;


I am not a Java Developer, I have zero experience

Any suggested method will be great as long as I have my desired result in python

Answer Source

When you invoke another process there is no returning of values.

The only thing you could hope for is a numerical return code value.

What you are actually looking for is this:

  • the program that runs in that process writes to either stdout ... or to some file
  • your python code that triggered that process can then read that content

See here for instructions how your python script can read what a subprocess is writing its stdout.

And for the record: there are no detours. If you intend to do anything "serious" with your java code, then you have to learn enough Java to make that happen. Not understanding that a method that says void can't have a return something statement is a good example for that.

Same is true for the python side of things. Yes, os.system() is the simply, straight forward way to invoke some process. But if you do even a tiny bit of research on that subject, you might have seen that there is the subprocess module in python ... together with its documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download