jsmubanlogo
  • 首页
  • 网页模板
  • 特效代码
  • 博文源码
  • 插件下载
  •    

java调用c++ exe并传参

收藏    

作者第十天    2021-12-09

      

参考博文:https://blog.csdn.net/hylbk/article/details/79100647
https://blog.csdn.net/weixin_43488958/article/details/106210988
Java的Runtime、Process类介绍及Runtime.getRuntime().exec方法的使用及问题:https://blog.csdn.net/qq_29550537/article/details/90244398

问题:

工程主体是java程序,现在需要调用一个由c++写成的算法,且这个算法的运行需要一些参数

解决思路:

  1. Java调用cpp.exe并传入参数
  2. cpp.exe执行将结果写入result.txt
  3. 最后在Java中读取result.txt中的结果即可

1. cpp.exe的实现

#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;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

2. Java程序的实现

方式一

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());
        }
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

exitValue方法使用于返回子进程的退出值,但是方法不会阻塞,也就是说在调用该方法时如果子进程尚未终止则会抛出IllegalThreadStateException异常。
如果需要等待子进程执行完毕,在继续运行程序,可以使用process.waitFor()方法,该方法是一个阻塞方法,直到process的自己进程运行结束才会返回,0表示正常终止。
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();
			}

		}
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

网上找的一个据说有解决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();
		}


免责声明:
      1、 资源售价只是赞助,不代表代码或者素材本身价格。收取费用仅维持本站的日常运营所需。
      2、 本站资源来自用户上传,仅供用户学习使用,不得用于商业或者非法用途,违反国家法律一切后果用户自负。用于商业用途,请购买正版授权合法使用。
      3、 本站资源不保证其完整性和安全性,下载后自行检测安全,在使用过程中出现的任何问题均与本站无关,本站不承担任何技术及版权问题,不对任何资源负法律责任。
      4、 如有损害你的权益,请联系275551777@qq.com及时删除。

关于我们 | 积分获取 | 联系我们 | 用户协议 | 标签搜索 | 网站地图.html | 网站地图.xml | 网站地图.txt

Copyright © 2021-2023 All Right Reserved
陕公网安备 61082202000148号      陕ICP备2025078528号-1
js模板网 -陕西千手码农科技有限责任公司