收藏
参考博文:https://blog.csdn.net/hylbk/article/details/79100647https://blog.csdn.net/weixin_43488958/article/details/106210988Java的Runtime、Process类介绍及Runtime.getRuntime().exec方法的使用及问题:https://blog.csdn.net/qq_29550537/article/details/90244398
问题:
工程主体是java程序,现在需要调用一个由c++写成的算法,且这个算法的运行需要一些参数
解决思路:
Java调用cpp.exe并传入参数cpp.exe执行将结果写入result.txt最后在Java中读取result.txt中的结果即可
#include <iostream> #include <fstream> using namespace std; int main(int argc, char** argv) { //argc是传入的参数个数 //argv是传入的参数列表 ofstream outfile; outfile.open("test001.txt"); //outfile.open("E://cppProject//test001.txt"); //注意这里中间必须是双杠 //argv[0]中存储的是java中传入的exe的路径,真正的参数存储从i=1开始 for(int i=1;i<argc;i++){ outfile<<argv[i]<<" "; } outfile<<endl; outfile.close(); return 0; }
public class Test { public static void main(String[] args) { final Runtime runtime = Runtime.getRuntime(); Process process = null; String cmd1="E:\\cppProject\\exeTest1\\exeTest1.exe"; //传入的执行文件路径 String x="2 9"; //与执行文件路径对应的参数 参数之间用空格隔开 try { process = new ProcessBuilder(cmd,sql).start(); System.out.println(process.isAlive()); //true //process.exitValue(); int exitVal = process.waitFor(); //阻塞程序直到进程运行完成,但容易引起死 process.destroy(); if(i==0) { System.out.println("子程序正常完成"); } else { System.out.println("出现异常"); } System.out.println(process.isAlive()); //false } catch(Exception e) { System.out.println(e.getMessage()); } } }
exitValue方法使用于返回子进程的退出值,但是方法不会阻塞,也就是说在调用该方法时如果子进程尚未终止则会抛出IllegalThreadStateException异常。如果需要等待子进程执行完毕,在继续运行程序,可以使用process.waitFor()方法,该方法是一个阻塞方法,直到process的自己进程运行结束才会返回,0表示正常终止。destroy()方法用于杀死子进程。isAlive()返回子进程的存活状态。
exitValue
IllegalThreadStateException
process.waitFor()
process
destroy()
isAlive()
Process process = null; String[] cmd = { "E:\\cppProject\\exeTest1\\exeTest1.exe","1","3" }; Runtime rn = Runtime.getRuntime(); { try { process = rn.exec(cmd); System.out.println("done--------------------"); int i=process.waitFor(); if(i==0) { System.out.println("子程序正常完成"); } else { System.out.println("出现异常"); } process.destroy(); } catch (IOException ioException) { ioException.printStackTrace(); } } }
网上找的一个据说有解决process.waitFor()死锁效果(目前还不太清楚)的一个方式:
String[] cmd = { "E:\\cppProject\\exeTest1\\exeTest1.exe", "1", "3" }; try { final Process process = Runtime.getRuntime().exec(cmd); // 处理InputStream的线程 new Thread() { @Override public void run() { BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; try { while ((line = in.readLine()) != null) { // System.out.println("output: " + line); } } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); new Thread() { @Override public void run() { BufferedReader err = new BufferedReader(new InputStreamReader(process.getErrorStream())); String line = null; try { while ((line = err.readLine()) != null) { System.out.println("err: " + line); } } catch (IOException e) { e.printStackTrace(); } finally { try { err.close(); } catch (IOException e) { e.printStackTrace(); } } } }.start(); process.waitFor(); System.out.println("执行完毕"); } catch (Exception e) { e.printStackTrace(); }