• <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>

            逛奔的蝸牛

            我不聰明,但我會(huì)很努力

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

            編譯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 逛奔的蝸牛 閱讀(2618) 評(píng)論(2)  編輯 收藏 引用 所屬分類: Java

            評(píng)論

            # 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ù)  更多評(píng)論
              

            # 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ù)  更多評(píng)論
              

            久久久无码一区二区三区| 亚洲午夜久久久久久久久久| 久久久精品久久久久特色影视| 色天使久久综合网天天| 日韩精品久久久肉伦网站 | 国内精品九九久久久精品| 久久综合综合久久97色| 2019久久久高清456| 精品久久久久久亚洲精品 | 久久久无码精品亚洲日韩按摩| 色综合久久久久| 亚洲狠狠婷婷综合久久久久| 久久综合狠狠色综合伊人| 久久久精品人妻一区二区三区蜜桃| 精品久久香蕉国产线看观看亚洲| 一本色综合久久| 91久久精品国产成人久久| 色综合久久中文字幕无码| 日本高清无卡码一区二区久久 | 久久精品国产亚洲AV香蕉| 伊人久久大香线蕉综合5g| 国产91久久综合| 久久精品国产精品亚洲精品| 久久99久久99精品免视看动漫| 久久无码精品一区二区三区| 国内精品伊人久久久久av一坑| 亚洲精品无码久久久久| 亚洲天堂久久久| 亚洲日本va午夜中文字幕久久 | 久久久久久久波多野结衣高潮| 国内精品欧美久久精品| 日本久久久精品中文字幕| 精品国产福利久久久| 久久Av无码精品人妻系列| 国产成人精品三上悠亚久久| 亚洲午夜福利精品久久| 无码8090精品久久一区 | 久久99国产精品久久久| 久久精品人人槡人妻人人玩AV| 久久婷婷国产综合精品| 久久国产精品99国产精|