gpt4 book ai didi

java - 通过命令行从java运行c/c++程序

转载 作者:行者123 更新时间:2023-11-30 09:35:38 25 4
gpt4 key购买 nike

我已经编写了一个编译和执行 C、c++、java 程序的 java 程序..我首先针对 java 测试它并且它工作得非常好。然后我用 C 测试了它,但它给出了错误。请告诉我需要做什么。这是编译代码的模块......:

public void compileCode(String path,String lang)throws IOException
{
String cmd="";
if(lang.equals("c")||lang.equals("cpp"))
cmd="g++ Main"+threadNum+"."+lang+" -o "+threadNum;
else if(lang.equals("java"))
cmd="javac Main"+threadNum+".java";

Process p=Runtime.getRuntime().exec(cmd,null,new File(path));

String s=null;
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdError.readLine()) != null) {
msg+=s+"\n";
res=0;
}
if(res!=0)
processCode(path,lang);
}

错误是:

Exception in thread "main" java.io.IOException: Cannot run program "g++" (in directory "C:\wamp\www\usercodes\lokesh"): CreateProcess error=2, The system cannot find the file specified
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)
at java.lang.Runtime.exec(Runtime.java:615)
at java.lang.Runtime.exec(Runtime.java:448)
at Contest.compileCode(Main.java:164)
at Contest.makeFile(Main.java:154)
at Contest.main(Main.java:52)
at Main.main(Main.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:1

20)

最佳答案

要使“g++”工作,Windows 的 PATH 中必须有一个 g++.exe。如果它是“g++.bat”或“g++.cmd”,则必须使用确切名称调用 Runtime.exec。

关于java - 通过命令行从java运行c/c++程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11268343/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com