• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            逛奔的蝸牛

            我不聰明,但我會很努力

               ::  :: 新隨筆 ::  ::  :: 管理 ::

            編譯Java程序:

            import java.io.FileNotFoundException;

            import java.io.PrintWriter;


            import com.sun.tools.javac.Main;


            public class Test {

                public static void main(String[] argv) throws FileNotFoundException {

                    PrintWriter writer = new PrintWriter("result.txt");

                    String[] options = { "/Users/Biao/Desktop/FilteredJList.java" };

                    Main.compile(options, writer);

                }

            }

            執(zhí)行Java程序:

            // Process proc = Runtime.getRuntime().exec(String.format("java %s", "FilteredJList"));

                private void run(String directory, String className, String[] args) {

                    try {

                        File classLoaderDirectory = new File(directory);

                        URL url = classLoaderDirectory.toURL();

                        URL[] urls = new URL[] { url };

                        ClassLoader loader = new URLClassLoader(urls);


                        Class clazz = loader.loadClass(className);


                        Method mainMethod = clazz.getMethod("main", String[].class);

                        // mainMethod.invoke(null, new Object[] { new String[] { /* args */}

                        // });

                        mainMethod.invoke(null, new Object[] { args });

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                }

             

            ================================分隔線================================

             

            JDK6.0中可以使用下面的方法:

            This example using the Java Compiler API introduced in JDK 1.6 to programmatically compile a Java class. Here we'll compile the Hello.java. The process of compiling can be start by obtaining a JavaCompiler from the ToolProvider.getSystemJavaCompiler().

            The simplest way to compile is by calling the run() method of the compiler and passing the first three arguments with null value. These three argument will use the default System.in,System.out and System.err. The final parameter is the file of the Java class to be compiled.

            When error happened during compilation process the non-zero result code will be returned. After the compile process you'll have the Hello.class just as if you were compiling using thejavac command.

            package org.kodejava.example.tools;
             
            import javax.tools.JavaCompiler;
            import javax.tools.ToolProvider;
             
            public class CompileHello {
            public static void main(String[] args) {
            JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
            int result = compiler.run(null, null, null,
            "src/org/kodejava/example/tools/Hello.java");
             
            System.out.println("Compile result code = " + result);
            }
            }

             


            posted on 2009-06-12 16:37 逛奔的蝸牛 閱讀(2619) 評論(2)  編輯 收藏 引用 所屬分類: Java

            評論

            # re: java: 在程序中編譯和執(zhí)行java程序 2009-06-12 16:56 暗金裝備
            還可以參考: http://konohaleaf.info/index.php?option=com_content&view=article&id=71:programmatically-compile-and-execute-in-java&catid=50:misc-example-programs&Itemid=64
              回復  更多評論
              

            # re: java: 在程序中編譯和執(zhí)行java程序 2011-05-11 19:41 jay_16
            Runtime run=Runtime.getRuntime();
            run.exec("cmd /c javac Calculator.java");
            run.exec("cmd /c java Calculator");
            Calculator是個有界面的程序,為什么編譯成功,卻運行不出來?
            Calculator程序文件在當前程序的目錄文件中的。  回復  更多評論
              

            久久青青草原综合伊人| 久久久久se色偷偷亚洲精品av| 精品少妇人妻av无码久久| 国产精品久久精品| 久久国产精品视频| 97精品依人久久久大香线蕉97| 精品久久久久中文字幕日本| 亚洲成人精品久久| 久久久久亚洲av成人网人人软件| 色欲综合久久中文字幕网| 久久久精品视频免费观看| 日产精品99久久久久久| 老司机午夜网站国内精品久久久久久久久| 四虎影视久久久免费观看| 久久99国产亚洲高清观看首页| 久久久久亚洲av成人无码电影 | 国产精品久久久久影院嫩草| 精品久久久久久无码免费| 亚洲国产精品无码久久久蜜芽| 亚洲国产精品久久66| 中文字幕久久久久人妻| 久久亚洲2019中文字幕| 蜜桃麻豆www久久| 国内精品九九久久久精品| 久久婷婷五月综合色奶水99啪| 久久综合九色综合精品| 99久久精品费精品国产一区二区| 欧美一区二区久久精品| 久久久中文字幕日本| 99久久精品国产毛片| 久久久精品人妻一区二区三区蜜桃 | 久久综合伊人77777麻豆| 精品乱码久久久久久夜夜嗨| 99久久国产热无码精品免费久久久久| av色综合久久天堂av色综合在| 一级a性色生活片久久无 | 老男人久久青草av高清| 久久人人爽人人爽人人片AV麻烦| 久久久久久av无码免费看大片| 国产精品免费看久久久香蕉| 国产精品久久久久久久久久免费|