臨時文件動態編譯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 相信 閱讀(612) 評論(0)  編輯  收藏 所屬分類: Swing文章

          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統計

          公告

          不顯示applet

          常用鏈接

          留言簿(16)

          我參與的團隊

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          swingchina 專業搞Swing的網站

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 芒康县| 北川| 奈曼旗| 抚远县| 宁蒗| 垦利县| 克东县| 瑞昌市| 运城市| 民县| 大丰市| 邯郸市| 淮北市| 海原县| 建阳市| 龙门县| 青海省| 张掖市| 清苑县| 铁岭县| 马鞍山市| 兴安县| 当雄县| 阜康市| 同心县| 迭部县| 花莲县| 新和县| 和田市| 桂阳县| 枣庄市| 海原县| 汝城县| 韶关市| 河南省| 光山县| 临漳县| 图木舒克市| 莱州市| 沽源县| 祁阳县|