1
我想在Struts2.0中創建一個包含textarea,屬性字段和提交按鈕的網頁。用戶將在這個文本區域輸入一個java代碼,我的代碼將編譯並執行它,並將在我的屬性字段中給出此代碼的結果...上面的代碼在獨立應用程序中正常工作。但它並未在我的Web應用程序中顯示任何內容。任何人都可以解決它...提前感謝。想從struts2網頁動態執行java類(其中包含main)
package org.controller;
import com.opensymphony.xwork2.ActionSupport;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
public class JCompilerAction extends ActionSupport
{
String program;
String MSg;
public JCompilerAction() {
}
public String getProgram() {
return program;
}
public void setProgram(String program) {
this.program = program;
}
public String getMSg() {
return MSg;
}
public void setMSg(String MSg) {
this.MSg = MSg;
}
public String Compile() {
try {
byte[] bFile = program.getBytes();
File f = new File("D:/nullprog.java");
FileOutputStream fileOuputStream = new FileOutputStream(f);
fileOuputStream.write(bFile);
fileOuputStream.close();
Process p1 = Runtime.getRuntime().exec("javac D:/nullprog.java");
BufferedReader in = new BufferedReader(new InputStreamReader(p1.getErrorStream()));
String line = null;
boolean isError = false;
while ((line = in.readLine()) != null) {
MSg = line;
isError = true;
return SUCCESS;
}
p1.waitFor();
if (!isError)
{
Process p2 = Runtime.getRuntime().exec("cmd /c start nullprog");
BufferedReader in1 = new BufferedReader(new InputStreamReader(p2.getInputStream()));
String line1 = null;
while ((line1 = in1.readLine()) != null) {
MSg += line1;
}
return SUCCESS;
}
} catch (Exception e) {
e.printStackTrace();
}
return SUCCESS;
}
public String execute() {
return SUCCESS;
}
}
_does是什麼意思不顯示任何東西?_。請更具描述性。 – Apurv 2013-03-25 04:18:05
當我在我的textarea中編寫錯誤的代碼(語法錯誤)時,我的編譯器代碼編譯它並在我的屬性字段中顯示相應的錯誤消息,即上面的代碼中的MSg,但是當我編寫正確的語法時,此代碼不生成輸出...既沒有在日誌中給出任何錯誤消息...這個相同的代碼在獨立模式下執行得很好... – 2013-03-25 04:29:16
爲什麼你不試試[JavaCompiler API](http://docs.oracle.com/javase /6/docs/api/javax/tools/JavaCompiler.html) – Apurv 2013-03-25 04:31:13