在项目中配置好启动路径后,启动项目就可以自启动elasticSearch,以后就不用手动双击启动了
public void run_cmd() {
String command = "cmd /c E:\\bin\\elasticSearch.bat";//这里改成自己实际的安装路径
try {
Runtime.getRuntime().exec(command);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
然后在运行项目前调用run_cmd,就可以启动了
public class EsApplication {
public static void main(String[] args) {
run_cmd();
SpringApplication.run(EsApplication.class, args);
}
}
上述代码基本实现了需要的功能
但多次运行,会导致端口占用,运行失败,所以可以进一步优化
《java多次自启动elasticSearch.bat文件,报错解决方案》