[英]Compiling a C file from a java Class
import java.io.*;
public class chk
{
String className;
String command,command1,command2;
public String getMsg(String fileName,File Path1)
{
String dir;
command = "tcc "+fileName;
String output = executeCommand(command,Path1);
if(output.compareTo("")==0)
output = "Compilation Successfull!!";
return output;
}
private String executeCommand(String command,File Path1)
{
StringBuffer output = new StringBuffer();
Process p;
try
{
p = Runtime.getRuntime().exec(command,null,Path1);
p.waitFor();
BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader1.readLine())!= null)
{
output.append(line + "\n");
}
while ((line = reader2.readLine())!= null)
{
output.append(line + "\n");
}
} catch (Exception e)
{
e.printStackTrace();
}
return output.toString();
}
public static void main(String args[])throws IOException
{
String x;
File dir=new File("D:\\test");
chk ob = new chk();
x = ob.getMsg("hello.c",dir);
System.out.println("OUtput : "+x);
}
}
ERROR 错误
I am trying to compile a C code from a java class. 我正在尝试从Java类编译C代码。 I am using Turbo C/C++ compiler and have also set its path ie "C:/TC/bin" even my programs are compiling when i compile them directly from command prompt but when i am trying to compile it with a java file the following error message appears.
我正在使用Turbo C / C ++编译器,并且还设置了它的路径,即“ C:/ TC / bin”,甚至当我从命令提示符直接编译它们时,我的程序也正在编译,但是当我尝试使用Java文件进行编译时,以下内容出现错误信息。 HELP!!
救命!!
Your code seems fine after adding a missing import: import java.io.*;
添加缺少的导入后,您的代码看起来很好:
import java.io.*;
however it seems you're using a very old compiler that's made for 16-bit /DOSWindows and that is probably the reason it won't work for you. 但是,似乎您使用的是针对16位/ DOSWindows的非常老的编译器,这可能是它对您不起作用的原因。
Try using a modern compiler like GCC instead, for Windows you'll want to use MinGW which is a version of the GCC compiler built for Windows. 尝试使用像GCC这样的现代编译器,对于Windows,您将要使用MinGW ,它是为Windows构建的GCC编译器的版本。 I tried your code using GCC 4.8.2 (MinGW) and it worked fine.
我使用GCC 4.8.2(MinGW)尝试了您的代码,并且工作正常。
Another alternative would be to use Microsofts Visual C++ compiler which also can be run from the command line, (but be aware that it only supports C89, with some features from later standards). 另一种选择是使用Microsoft的Visual C ++编译器,该编译器也可以从命令行运行(但请注意,它仅支持C89,并且具有更高版本的标准功能)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.