• <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 逛奔的蝸牛 閱讀(2624) 評論(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
              回復(fù)  更多評論
              

            # 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是個(gè)有界面的程序,為什么編譯成功,卻運(yùn)行不出來?
            Calculator程序文件在當(dāng)前程序的目錄文件中的。  回復(fù)  更多評論
              

            久久亚洲高清观看| 精品无码久久久久久午夜| 久久久老熟女一区二区三区| 色综合久久久久久久久五月| 97久久精品国产精品青草| 三级片免费观看久久| 午夜精品久久久久久99热| 91精品国产高清久久久久久io| 成人国内精品久久久久一区| 国产精品热久久无码av| 亚洲综合精品香蕉久久网| 91久久精品电影| 亚洲AV无码久久精品成人| 久久精品免费一区二区三区| 欧美熟妇另类久久久久久不卡| 久久天天躁狠狠躁夜夜不卡| 久久777国产线看观看精品| 久久综合狠狠综合久久97色| 久久亚洲AV成人出白浆无码国产| 久久精品一区二区三区中文字幕| 久久99热狠狠色精品一区| 久久精品免费全国观看国产| 热久久这里只有精品| 久久福利资源国产精品999| 91精品无码久久久久久五月天 | 久久天天躁狠狠躁夜夜躁2O2O| 色综合久久久久网| 久久精品无码一区二区无码| 久久久午夜精品| 久久精品亚洲欧美日韩久久| 国产高潮久久免费观看| 久久精品一区二区三区不卡| 中文字幕乱码久久午夜| 久久人与动人物a级毛片| 无码人妻久久一区二区三区蜜桃 | 久久久不卡国产精品一区二区| 久久精品国产亚洲AV电影 | 久久精品欧美日韩精品| 亚洲精品白浆高清久久久久久| 久久亚洲AV无码精品色午夜| 无码人妻久久一区二区三区蜜桃|