如何在Java中使用Python代码中的方法?

lpwwtiir  于 2023-01-07  发布在  Java
关注(0)|答案(2)|浏览(107)

我有一个Python脚本,看起来像这样:

def methodOne():
    print("Hello I am the first method")

def methodTwo():
    print("Hello I am the second method")

我有一个简单的Java脚本,如下所示:

public class helloworld {

    public static void main(String[] args) {
        //Here I want to call the Methods that are in the Python script
    }

}

但是我想不出如何在Java文件中调用那些方法,有没有办法做到这一点?

h79rfbju

h79rfbju1#

您可以使用Java Runtime. exec()来运行python脚本。例如,首先使用shebang创建python脚本文件,然后将其设置为可执行文件

xytpbqjk

xytpbqjk2#

试试这个,我的朋友https://pythonhosted.org/javabridge/java2python.html,它可能会帮助你解决这个问题。但是我从来没有试过我自己

相关问题