2013-03-25 58 views
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; 
} 
} 
+0

_does是什麼意思不顯示任何東西?_。請更具描述性。 – Apurv 2013-03-25 04:18:05

+0

當我在我的textarea中編寫錯誤的代碼(語法錯誤)時,我的編譯器代碼編譯它並在我的屬性字段中顯示相應的錯誤消息,即上面的代碼中的MSg,但是當我編寫正確的語法時,此代碼不生成輸出...既沒有在日誌中給出任何錯誤消息...這個相同的代碼在獨立模式下執行得很好... – 2013-03-25 04:29:16

+0

爲什麼你不試試[JavaCompiler API](http://docs.oracle.com/javase /6/docs/api/javax/tools/JavaCompiler.html) – Apurv 2013-03-25 04:31:13

回答

0

下面是一個簡單的程序,首先編譯代碼,然後執行它:

Executer.java

import java.lang.reflect.Method; 

public class Executer { 
    public static void main(String args[]) { 
     try{ 
      java.lang.Process p1 = Runtime.getRuntime().exec("javac MyClass.java"); 
      p1.waitFor(); 
      Class<?> c = Class.forName("MyClass"); 
      Object obj = c.newInstance(); 
      Method[] mArr = obj.getClass().getMethods(); 
      for(Method m : mArr){ 
       if(m.getName().equalsIgnoreCase("main")){ 
        m.invoke(obj, new Object[]{new String[]{}}); 
       } 
      } 
     } catch (Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 

MyClass.java

import javax.swing.JOptionPane; 

public class MyClass { 
    public static void main(String[] args) { 
     JOptionPane.showMessageDialog(null, "Hii"); 
    } 
} 

爲了簡單起見b其他課程都在同一個包裏。當我執行Executer類時,它首先編譯MyClass.java,然後運行主要方法。

如果您的要求是將某些文件夾中的Java文件放在項目之外,要加載編譯器類文件,您需要按照this中提到的方法遵循答案。