在C ++中分配一个新进程并执行.jar文件 [英] Forking a new process in C++ and executing a .jar file

查看:184
本文介绍了在C ++中分配一个新进程并执行.jar文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,它将从java .jar文件中读取输出,并且不时地给它输入。基本上,我希望能够创建一个程序,当遇到某些输出时执行某些函数。



.jar文件不是由我创建的,我不是有源代码,所以我必须使用它作为。在做了一些研究后,我决定使用 fork() execl()使用和我创建了一个小程序,成功地做了这个与hello世界程序。正如我正准备修改它运行.jar程序,我意识到.jar是不能自己执行,我必须通过java执行它,所以现在我无法弄清楚如何使 execl()使用.jar。



我试图找出如何使execl并使用指定要执行的jar文件的参数:

  execl(java,java,jar myprog.jar,NULL); 

我不知道这个方法是否可以工作。如果它会我不知道使用什么路径。我看到一些人推荐使用JNI类似的目的,但没有一个似乎与我正在做的事情,后研究了一点,我不能完全确定这将是最好的方式。 popen()似乎是另一种可能性,但我还没有找到解释如何使用它的任何东西。



任何建议都将非常感激。 b $ b

解决方案

找到java可执行文件:如果你想在PATH中找到它,你可以使用execlp而不是execl。否则,您可以使其可配置(配置文件或环境变量)。对于jar文件,您可以在当前目录或再次可配置的位置查找它。



例如,对于在PATH中的java和当前工作目录中的myprog.jar:



ret = execlp(java,java,-jar,myprog.jar,(char *)0);


I am attempting to write a program that will read the output from a java .jar file and also give it input from time to time. Basically I am hoping to make a program that will execute certain functions when certain output is encountered.

The .jar file wasn't created by me, and I don't have the source code, so I have to use it as-is. After doing some research I decided using fork() and execl() in conjunction with pipes was the method I would need to use and I created a small program that successfully does this with a hello world program. Just as I was getting ready to modify it to run the .jar program I realized that the .jar isn't executable on its own, I have to execute it through java and so now I'm unable to figure out how to make execl() work with the .jar.

I have tried to figure out how to make execl() run java and use an argument that specifies the jar file to execute like so:

execl("java","java","jar myprog.jar",NULL);

I don't know if this method will work, though. If it will I don't know what path to use. I have seen some people recommend using JNI for similar purposes, but none of them seemed to quite fit with what I was doing and after researching it a bit I'm not totally sure that would be the best way to go. popen() seems like another possibility, but I have yet to find anything that explains how to use it well.

Any advice would be greatly appreciated.

解决方案

For finding the java executable: if you want to find it in PATH you can use execlp instead of execl. Otherwise you can make it configurable (configuration file or env. variable). For the jar file you can look for it in the current directory or again a configurable location.

E.g., for java in PATH and myprog.jar in the current working directory:

ret = execlp("java", "java", "-jar", "myprog.jar", (char *)0);

这篇关于在C ++中分配一个新进程并执行.jar文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆