java实现c,c++,java代码编译器
java 实现c,c++,java代码编译器
最近在做⼀个onlinejudge的项⽬,⾸先最头疼的就是实现代码编辑器,我的需求是:想让⽤户可以在⽹站⾥写代码,然后实现在线编译,可以⾃⼰输⼊测试⽤例,如果报错,则提⽰错误信息
最先想到的是cmd编译程序,拿java来举例,⼀般写好的java程序,想要编译它,并获得结果,⾸先得有jdk环境,然后可以在cmd⾥使⽤javac命令来编译java⽂件,然后得到⼀个class⽂件,最后再使⽤java命令来运⾏class⾥具体的类,当然,java⽂件的名字就是class名,这样就可以实现编译java⽂件的操作,并获得结果!
那么同理:c语⾔就是gcc命令编译c⽂件⽣成exe⽂件,c++就是g++命令编译cpp⽂件⽣成exe⽂件(前提电脑得有这些环境,没有环境的,去百度⼀下吧!!)
那么现在的问题就转换为了java操作cmd命令了,然后就会想到Runtime().exec()
但⼜有个问题:从上⾯的分析看出,怎么也得调⽤俩条cmd命令,但Runtime().exec()是异步执⾏的,我想要它⼀条命令执⾏完之后再执⾏第⼆条命令,活⼈怎么能被尿憋死呢!!给第⼀条命令开⼀个线程,这样等第⼀条命令执⾏完之后,再执⾏第⼆条命令即可
这⾥需要⼀个临时的.c,.cpp,.java⽂件来储存临时代码,所有需要写⼀个⽂件操作⼯具类FileUtil
上代码:
c语⾔编译器
package complierplier_c;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
/
**
* C编译器
* @author tpw
*
*/
public class CComplier {
private final static String Property("user.dir")+"\\cCode\\";
private final static String CplusplusFILENAME="main.c";
private final static String CplusplusEXE="";
private String inputData;//输⼊数据
private String outputData;//输出数据
private String errorMsg;//程序错误信息
public CComplier(){
this.inputData="";
}
public CComplier(String inputData){
super();
this.inputData = inputData;
}
public CComplier(String inputData, String outputData, String errorMsg){
super();
this.inputData = inputData;
this.outputData = outputData;
}
public void complier(){
// 进⼊c代码存放⽂件夹
// 使⽤gcc命令来编译c⽂件,来⽣成⼀个exe⽂件
// 直接运⾏exe⽂件
// cmd命令:cd /d d:\javaworkspace\complier\cCode
// cmd命令:gcc main.cpp
/
/ cmd命令:
StringBuffer errorInfo =new StringBuffer();
Process p=null;
try{
//1.编译C++⽂件
p = Runtime().exec("gcc "+CplusplusFILEPATH+CplusplusFILENAME+" -o "+CplusplusFILEPATH+"main");
jdk怎么使用// 获取进程的错误流
final InputStream is1 = p.getErrorStream();
// 开⼀个线程,读标准错误流
new Thread(){
public void run(){
try{
BufferedReader br1 =new BufferedReader(new InputStreamReader(is1,Charset.forName("GBK")));
String line1 = null;
while((line1 = adLine())!= null){
if(line1 != null){
errorInfo.append(line1 +"\n");
}
}
if(!String().equals("")){
String();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
is1.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}.start();
p.waitFor();
p.destroy();
//2.如果没错的话就运⾏exe⽂件
String().equals("")){
try{
Process process = Runtime().exec(CplusplusFILEPATH+CplusplusEXE);
BufferedWriter bout =new BufferedWriter(new OutputStream()));
bout.write(this.inputData);
bout.close();
BufferedReader br =new BufferedReader(new InputStream(),Charset.forName("GBK")));    String line = null;
StringBuffer b =new StringBuffer();
while((line = br.readLine())!= null){
b.append(line +"\n");
}
this.String();
}catch(IOException e){
e.printStackTrace();
}
}
}catch(Exception e){
try{
}catch(Exception ee){
ee.printStackTrace();
}
}
}
public String getInputData(){
return inputData;
}
public void setInputData(String inputData){
this.inputData = inputData;
}
public String getOutputData(){
return outputData;
}
public void setOutputData(String outputData){
this.outputData = outputData;
}
public String getErrorMsg(){
return errorMsg;
}
public void setErrorMsg(String errorMsg){
}
}
c++编译器
package complierplier_cplusplus;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
/**
* C++编译器
* @author tpw
*
*/
public class CplusplusComplier {
private final static String Property("user.dir")+"\\c++Code\\"; private final static String CplusplusFILENAME="main.cpp";
private final static String CplusplusEXE="";
private String inputData;//输⼊数据
private String outputData;//输出数据
private String errorMsg;//程序错误信息
public CplusplusComplier(){
this.inputData="";
}
public CplusplusComplier(String inputData){
super();
this.inputData = inputData;
}
public CplusplusComplier(String inputData, String outputData, String errorMsg){
super();
this.inputData = inputData;
this.outputData = outputData;
}
public void complier(){
// 进⼊c++代码存放⽂件夹
// 使⽤g++命令来编译c++⽂件,来⽣成⼀个exe⽂件
// 直接运⾏exe⽂件
// cmd命令:cd /d d:\javaworkspace\complier\c++Code
/
/ cmd命令:g++ main.cpp
// cmd命令:
StringBuffer errorInfo =new StringBuffer();
Process p=null;
try{
//1.编译C++⽂件
p = Runtime().exec("g++ "+CplusplusFILEPATH+CplusplusFILENAME+" -o "+CplusplusFILEPATH+"main");
// 获取进程的错误流
final InputStream is1 = p.getErrorStream();
// 开⼀个线程,读标准错误流
new Thread(){
public void run(){
try{
BufferedReader br1 =new BufferedReader(new InputStreamReader(is1,Charset.forName("GBK")));
String line1 = null;
while((line1 = adLine())!= null){
if(line1 != null){
errorInfo.append(line1 +"\n");
}
}
if(!String().equals("")){
String();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
is1.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}.start();
p.waitFor();
p.destroy();
//2.如果没错的话就运⾏exe⽂件
String().equals("")){
try{
Process process = Runtime().exec(CplusplusFILEPATH+CplusplusEXE);
BufferedWriter bout =new BufferedWriter(new OutputStream()));
bout.write(this.inputData);
bout.close();
BufferedReader br =new BufferedReader(new InputStream(),Charset.forName("GBK")));    String line = null;
StringBuffer b =new StringBuffer();
while((line = br.readLine())!= null){
b.append(line +"\n");
}
this.String();
}catch(IOException e){
e.printStackTrace();
}
}
}catch(Exception e){
try{
}catch(Exception ee){
ee.printStackTrace();
}
}
}
public String getInputData(){
return inputData;
}
public void setInputData(String inputData){
this.inputData = inputData;
}
public String getOutputData(){
return outputData;
}
public void setOutputData(String outputData){
this.outputData = outputData;
}
public String getErrorMsg(){
return errorMsg;
}
public void setErrorMsg(String errorMsg){
}
}
java编译器
package complierplier_java;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
/**
* java编译器
* @author tpw
*
*/
public class JavaComplier {
private final static String Property("user.dir")+"\\javaCode"; private final static String JAVAFILENAME="Main.java";
private final static String jAVAFILECLASNAME="Main";
private String inputData;//输⼊数据
private String outputData;//输出数据
private String errorMsg;//程序错误信息
public JavaComplier(){
this.inputData="";

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。