gpt4 book ai didi

java - 如何从 java 代码编译和运行 java 和 C 文件?

转载 作者:搜寻专家 更新时间:2023-11-01 03:27:45 24 4
gpt4 key购买 nike

我正在开发一个编辑器应用程序,我可以在其中编译和运行 c、cpp 和 Java 文件。我正在使用 java 编程语言开发此应用程序。我正在 Eclipse 中开发它。我可以在特定位置创建新文件(c、cpp 和 java),也可以将文件保存到不同的 2 个位置。为了执行,我使用了以下方法。

String compileFileCommand = "javac "+fileName;

Process compile_process = new ProcessBuilder(compileFileCommand).redirectErrorStream(true).start();
compile_process.waitFor();

BufferedReader reader=new BufferedReader(new InputStreamReader(compile_process.getInputStream()));
String line=reader.readLine();
while(line!=null) {
System.out.println(line);
line=reader.readLine();
}

我的问题是我无法从相应位置编译文件。

总是给出异常

java.io.IOException: error=2, No such file or directory 

请告诉我如何编译和运行所有 c、c++ 和 java 文件。还请对我的申请提出任何其他建议。

编辑..我已经使用这两种方法进行编译和运行。在编译时,它会在 Java 的情况下创建一个类文件。但我一直从 InputStreams(getErrorStream() 和 getInputStream())中获取 null。

void compileJavaFile(String fileName)
{
String compileFileCommand = "javac " + fileName;
try
{
System.out.println("Executing Java File");

Process compileProcess = Runtime.getRuntime().exec(compileFileCommand);

String line = "";
BufferedReader bri = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
BufferedReader bre = new BufferedReader(new InputStreamReader(compileProcess.getErrorStream()));
while ((line = bri.readLine()) != null)
{
System.out.println(line);
}
bri.close();
while ((line = bre.readLine()) != null)
{
System.out.println(line);
}
bre.close();
compileProcess.waitFor();
System.out.println("Done.");
} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}

void runJavaFile(String fileName)
{
String runFileCommand = "java " + fileName.split(".java")[0];
try
{
System.out.println("runFileCommand : " + runFileCommand);
System.out.println("Running Java File");

Process runProcess = Runtime.getRuntime().exec(runFileCommand);

BufferedReader reader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
String line = reader.readLine();
System.out.println("line = " + line);
while (line != null)
{
System.out.println(line);
line = reader.readLine();
}

} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}

对于 C 和 C++,我正在使用。

void compileCFile(String fileName)
{
String compileFileCommand = "gcc " + fileName;

resultString = "";
try
{
System.out.println("Compiling C File");

Process processCompile = Runtime.getRuntime().exec(compileFileCommand);

BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String errorCompile = brCompileError.readLine();
if (errorCompile != null)
System.out.println("Error Compiler = " + errorCompile);

resultString += errorCompile +"\n";

BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String outputCompile = brCompileRun.readLine();
if (outputCompile != null)
System.out.println("Output Compiler = " + outputCompile);

resultString += outputCompile +"\n";

} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}

void runCFile(String fileName)
{
String runFileCommand = "./" + fileName.split(".c")[0];

try
{

System.out.println("Running C File");

Process processRun = Runtime.getRuntime().exec(runFileCommand);

BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
String errorRun = brRun.readLine();
if (errorRun != null)
System.out.println("Error Run = " + errorRun);

BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String outputRun = brResult.readLine();
if (outputRun != null)
System.out.println("Output Run = " + outputRun);

} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}

void compileCPPFile(String fileName)
{
String compileFileCommand = "g++ " + fileName;
try
{

System.out.println("Compiling CPP File");

Process processCompile = Runtime.getRuntime().exec(compileFileCommand);

BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String errorCompile = brCompileError.readLine();
if (errorCompile != null)
System.out.println("Error Compiler = " + errorCompile);

resultString += errorCompile +"\n";

BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
String outputCompile = brCompileRun.readLine();
if (outputCompile != null)
System.out.println("Output Compiler = " + outputCompile);

resultString += outputCompile +"\n";

} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}

void runCPPFile(String fileName)
{
String runFileCommand = "./" + fileName.split(".cpp")[0];

try
{
System.out.println("Running CPP File");

Process processRun = Runtime.getRuntime().exec(runFileCommand);

BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
String errorRun = brRun.readLine();
if (errorRun != null)
System.out.println("Error Run = " + errorRun);

BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
String outputRun = brResult.readLine();
if (outputRun != null)
System.out.println("Output Run = " + outputRun);

} catch (Exception e)
{
// TODO: handle exception
System.out.println("Exception ");
System.out.println(e.getMessage());
}
}

如果是 C 和 C++,它会显示如下错误

g++: /media/disk/eclipse/\/UniversalIDE/CPP/firstCPP: No such file or directory

请给我解决我的问题..

最佳答案

请尝试以下,

Process p = Runtime.getRuntime().exec(command);

command 是您传递的字符串。在命令中,您可以传递“javac Test.java”来编译您的 java 文件,就像您可以使用其他命令一样。

关于java - 如何从 java 代码编译和运行 java 和 C 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8999075/

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