简体   繁体   English

从Java运行C ++问题

[英]Running c++ from Java problem

I need to compile and run a c++ program from java. 我需要从Java编译并运行c ++程序。 I am using 我在用

Process a = Runtime.getRuntime().exec ("g++ -g function.cpp -o function"); 处理a = Runtime.getRuntime()。exec(“ g ++ -g function.cpp -o function”); Process b = Runtime.getRuntime().exec ("./function"); 进程b = Runtime.getRuntime()。exec(“ ./function”);

the problem is that the output I get from the c++ program is not correct but If I compile and run it myself in the command line it works just fine. 问题是我从c ++程序获得的输出不正确,但是如果我自己在命令行中编译并运行它,则效果很好。 The problem is Java and i dont know why. 问题是Java,我不知道为什么。

Thanks a lot 非常感谢

Al,

There is one definite and one probable problem that I see here. 我在这里看到一个明确的问题和一个可能的问题。 The definite problem is that Runtime.exec() does not wait for the process to complete. 该明确的问题是,的Runtime.exec() 等待过程来完成。 So you will need to add 因此,您需要添加

a.waitFor();

before calling b. 致电之前b。

The possible issue is that depending on how you are invoking this application, the current working directory may not be where you think it is. 可能的问题是,取决于您如何调用此应用程序,当前的工作目录可能不在您认为的位置。 So function.cpp may not exist. 因此function.cpp可能不存在。

您是否正在等待进程A完成,然后再运行进程B?

"The output... is not correct" doesn't help anyone to diagnose your issue. “输出...不正确”不会帮助任何人诊断您的问题。 You should definitely give the output you expected, and the output you saw from Java. 您绝对应该提供期望的输出以及从Java中看到的输出。 Assuming that your program is small, you should post the source code of that too (since this is about the compilation process after all). 假设您的程序很小,那么您也应该发布该程序的源代码(因为这毕竟与编译过程有关)。

By the way, what happens when you navigate to the working direction of the Java program, find the function executable it generating and invoke that yourself from the command line? 顺便说一句,当您导航到Java程序的工作方向,找到它生成的可执行文件并从命令行调用该function会发生什么? Is the output correct now? 现在输出正确吗? The answer to this will let you know whether the problem is in the compilation step or the execution step. 答案将使您知道问题是在编译步骤还是在执行步骤。

If it's execution, I would hazard a guess at things like the environment (envvars, PATH, etc.) but without more information it's hard to tell. 如果执行,我会冒险猜测环境(envvars,PATH等)之类的东西,但是如果没有更多信息,这很难说。

Also, as with all question that involve Process es, take a look at these common pitfalls . 另外,与涉及Process es的所有问题一样,请看一下这些常见的陷阱 It looks like you're making at least one of them (the common one of not consuming output) which might lead to your program working on trivial C++ code but deadlocking on a larger codebase. 看来您正在制作其中至少一个(不消耗输出的常见之一),这可能会导致您的程序在琐碎的C ++代码上工作,但在更大的代码库上陷入僵局。

You're also not checking the output (either the return value or the stdout/stderr streams) of the compilation step at all, so you have no idea whether the compilation was successful - and if not, what (useful) error messages you got from the compiler. 您也根本不检查编译步骤的输出(返回值或stdout / stderr流),因此您不知道编译是否成功-如果不成功,您将得到什么(有用的)错误消息从编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM