-2

我将我的应用程序开发分为三个模块,需要用 2 种语言(Python 和 java)完成我已经在 python 中开发了一个脚本,用于从汇合页面下载附件,但不幸的是我不得不在 JAVA 中开发另一个模块。我需要在 python 脚本完成执行后调用 java 实例。可能吗 ?。

4

1 回答 1

0

如果您将 Spring 与 Java 一起使用,则可以在运行 python 脚本的地方使用 @PostConstruct 注释。组件初始化后调用的带注释的方法。举个例子:

import javax.annotation.PostConstruct;

import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;

@Component
public class MyAmazingComponent {


    @PostConstruct
    private void callScript() {
        Process process;
        try{
            process = Runtime.getRuntime().exec(new String[]{"path/script_python.py","arg1","arg2"});
        
        }catch(Exception e) {
            System.out.println("Exception " + e.toString());
        }

     }
    
    public void doSomething(){...}

}

以上代码需要以下依赖项(如果您当然使用 maven 并且版本不是必须的,您可以更改它们):

<dependency>
    <groupId>javax.annotation</groupId>
    <artifactId>javax.annotation-api</artifactId>
    <version>1.3.2</version>
</dependency>

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.6.RELEASE</version>
</dependency>
于 2021-02-08T12:46:47.467 回答