执行python脚本后执行Java模块?



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

如果你在Java中使用Spring,你可以在运行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>