mee
mee

Reputation: 718

Instantiate a java class in python and send it back to java

I have a class in java:

public class MyClass{
    public String a=null;
    public String b=null;
    public String c=null;
    public String d=null;
    public static String testMyClass() {
        //my method instruction using the attributes a,b,c and d
    }

The trouble is that MyClass attributes must be received from python to be able to use the method testMyClass. So is it possible to instantiate a java class from python and after send it back to java using py4j? Anyone can show how to make it if it is possible? Or have another alternative?

Upvotes: 1

Views: 186

Answers (1)

Sree Kumar
Sree Kumar

Reputation: 2245

I don't think that is possible unless someone knows of a "bridge" utility between Java and Python.

However, you may do this:

  1. Create a JSON in the Python script in a structure that matches that of MyClass
  2. Receive it in your Java method and deserialize the JSON into an instance of MyClass using Jackson or some JSON tool.

Your next problem will be the communication between you Java process and Python process. For that, see if this StackOverflow question helps: Passing data from Java process to a python script

Upvotes: 2

Related Questions