How to call java from C++

前端 未结 4 1928
遇见更好的自我
遇见更好的自我 2020-12-21 17:27

I need to run this line from my c++ program:

java -jar test.jar text1 text2

the java app will give a float value and give it to the c++ program.

How

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 17:56

    The easiest if You can modify your java code:

    write the result to environment variable (pseudo code below):

    solution 1. (Write directly to env. in java app.)

    java:

    ...
    setenv('ret', somefloatvalue);
    ...
    
    exit..
    

    c++:

    system("java -jar test.jar text1 text2")
    ...
    getenv("ret")
    

    (I haven't test it, but important here is the context, does system creates another shell (console), if yes, you'll not see those envs, therefore some other spawn method is necessary)

    CreateProcess() on windows fork() on linux.

    There are also more complex solutions,

    • send some JASON's through the sockets.... etc.
    • Write to text file in java, read in c++.
    • MPI...
    • extreme in this case CORBA ;)

提交回复
热议问题