臨時(shí)文件動(dòng)態(tài)編譯java代碼

          本程序出自群里.22069957 之前用到這個(gè)功能在網(wǎng)頁上動(dòng)態(tài)生成用戶寫的代碼..共享給大家.


          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 相信 閱讀(609) 評(píng)論(0)  編輯  收藏 所屬分類: Swing文章

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

          導(dǎo)航

          統(tǒng)計(jì)

          公告

          不顯示applet

          常用鏈接

          留言簿(16)

          我參與的團(tuán)隊(duì)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊(cè)

          swingchina 專業(yè)搞Swing的網(wǎng)站

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 南部县| 伊吾县| 苍梧县| 临城县| 儋州市| 静海县| 松江区| 闽清县| 武冈市| 邵阳市| 昭觉县| 玛纳斯县| 望谟县| 永胜县| 安岳县| 贵港市| 聊城市| 博兴县| 松江区| 拜城县| 定结县| 九江县| 且末县| 梅州市| 高阳县| 蒙自县| 阿荣旗| 湘乡市| 秦安县| 偃师市| 绥阳县| 阿克陶县| 新安县| 九龙坡区| 荃湾区| 甘南县| 抚松县| 慈溪市| 东台市| 霍山县| 靖安县|