臨時文件動態編譯java代碼

          本程序出自群里.22069957 之前用到這個功能在網頁上動態生成用戶寫的代碼..共享給大家.


          import java.io.File;
          import java.io.PrintWriter;
          import java.net.URL;
          import java.net.URLClassLoader;
          import java.util.Arrays;

          import javax.swing.JButton;
          import javax.swing.JFrame;
          import javax.swing.SwingUtilities;
          import javax.tools.DiagnosticCollector;
          import javax.tools.JavaCompiler;
          import javax.tools.StandardJavaFileManager;
          import javax.tools.ToolProvider;

          public class demo {
              
          public demo(String s) {
                  
          try {

                      System.getProperties().store(System.out, 
          "");

                      JavaCompiler compiler 
          = ToolProvider.getSystemJavaCompiler();
                      DiagnosticCollector diagnostics 
          = new DiagnosticCollector();
                      StandardJavaFileManager fileManager 
          = compiler
                              .getStandardFileManager(diagnostics, 
          nullnull);
                      String fileName 
          = "MyButton.java";

                      File file 
          = new File(System.getProperty("java.io.tmpdir"), fileName);
                      PrintWriter pw 
          = new PrintWriter(file);
                      pw.println(s);
                      pw.close();

                      Iterable compilationUnits 
          = fileManager
                              .getJavaFileObjectsFromStrings(Arrays.asList(file
                                      .getAbsolutePath()));
                      JavaCompiler.CompilationTask task 
          = compiler.getTask(null,
                              fileManager, diagnostics, 
          nullnull, compilationUnits);
                      
          boolean success = task.call();
                      fileManager.close();
                      System.out.println((success) 
          ? "編譯成功" : "編譯失敗");
                      URLClassLoader classLoader 
          = new URLClassLoader(
                              
          new URL[] new File(System.getProperty("java.io.tmpdir"))
                                      .toURI().toURL() }
          );
                      
          final JButton btn = (JButton) classLoader.loadClass("MyButton")
                              .newInstance();
                      btn.setText(
          "按鈕");
                      System.out.println(btn);
                      SwingUtilities.invokeLater(
          new Runnable() {
                          @Override
                          
          public void run() {
                              JFrame frame 
          = new JFrame("frame");
                              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                              frame.setSize(
          640480);
                              frame.setLocationRelativeTo(
          null);
                              frame.getContentPane().add(btn);
                              frame.setVisible(
          true);
                          }

                      }
          );
                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              
          public static void main(String args[]) {
                  
          new demo(
                          
          "import javax.swing.JButton; public class MyButton extends JButton {}");
              }

          }

          posted on 2010-02-04 23:49 相信 閱讀(608) 評論(0)  編輯  收藏 所屬分類: Swing文章

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

          不顯示applet

          常用鏈接

          留言簿(16)

          我參與的團隊

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          swingchina 專業搞Swing的網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 大冶市| 和平区| 潞西市| 咸宁市| 双流县| 隆安县| 黄浦区| 定安县| 关岭| 招远市| 惠水县| 和静县| 高州市| 古浪县| 尚义县| 江城| 屯留县| 韩城市| 商南县| 洛南县| 元江| 化隆| 白玉县| 通海县| 陕西省| 北流市| 济源市| 皋兰县| 昭平县| 和田县| 全南县| 丹东市| 抚顺县| 图木舒克市| 白河县| 财经| 诸城市| 潮安县| 土默特右旗| 广南县| 威信县|