从导致问题的java进程附加到外部c++进程

Attach to external c++ process from java process causing issue

本文关键字:进程 外部 c++ java 问题      更新时间:2023-10-16

我正在尝试使用java.lang.prrocess附加C++可执行文件。构建exec的代码如下:

int main(int, char**){
std::cout << "Starting Up. . . . . " << std::endl;
std::string command;
while (command != "exit")
{
    std::cin >> command;
}
return 0;
}

在调试过程中,我发现在进程的创建过程中,进程在std::cin上停止,并期望输入值,但在下一次迭代中,它会自动获取上一个"命令"std::cin值,并继续迭代"While"循环,而不会将控制权交回java进程。为什么c++可执行文件在每次迭代时都不会在std::cin处停止?我正在使用Process.getOutputStream()传递来自java的值。如果描述有任何问题,请告诉我。谢谢,托尔。

很可能没有有效的输入或模拟了某种EOF。您应该检查输入是否有错误:

while ( std::cin && command != "exit" )
{
    std::cin >> command;
}