動態產生JAVA代碼(三)

來源:互聯網
上載者:User

該例不是直接使用javac,而是使用tools.jar

/*
?* 建立日期 2004-7-2
?* 建立人 HongSoft
?* 檔案名稱 TestCompile.java
?*/
package com.hongsoft.test;

import java.io.*;

//定製的類裝入器
public class MyCompile extends ClassLoader
{
?String _classpath;

?public static void main(String[] args)
?{

??new TestCompile();
?}

?public MyCompile()
?{
??super(ClassLoader.getSystemClassLoader());
??//編譯器類型??
??_classpath = ".";
??String extraclasspath =
???"c://Program Files//Java//j2re1.4.2//lib//rt.jar";
??// = System.getProperty("calc.classpath");
??if (extraclasspath != null)
??{
???_classpath =
????_classpath
?????+ System.getProperty("path.separator")
?????+ extraclasspath;
??}
??compile();
?}

?public void compile()
?{
??// A3???????
??String filename = "";
??String classname = "";
??try
??{
???//建立臨時檔案
???File javafile =
????File.createTempFile("compiled_", ".java", new File("."));
???filename = javafile.getName();
???classname = filename.substring(0, filename.lastIndexOf("."));
???generateJavaFile(javafile, classname);
???//編譯檔案
???invokeCompiler(javafile);
???//建立java類
???byte[] buf = readBytes("d://" + classname + ".class");
???Class c = defineClass(buf, 0, buf.length);
???try
???{
????// 建立並返回類的執行個體
????//return (Calculator)
????c.newInstance();
???}
???catch (IllegalAccessException e)
???{
????throw new RuntimeException(e.getMessage());
???}
???catch (InstantiationException e)
???{
????throw new RuntimeException(e.getMessage());
???}
??}
??catch (IOException e)
??{
???throw new RuntimeException(e.getMessage());
??}
?}

?//產生java檔案
?void generateJavaFile(File javafile, String classname) throws IOException
?{
??FileOutputStream out = new FileOutputStream(javafile);
??String text =
???"public class "
????+ classname
????+ " {"
????+ " public int getCreater() {return 1;}"
????+ "}";
??out.write(text.getBytes());
??out.close();
?}

?//編譯java檔案
?void invokeCompiler(File javafile) throws IOException
?{
??String[] cmd =
???{ "-classpath", _classpath, "-d", "d://", javafile.getName()};
??//執行編譯命令
??int val = new com.sun.tools.javac.Main().compile(cmd);

??if (val != 0)
??{
???throw new RuntimeException("編譯錯誤:" + "錯誤碼" + val);
??}
?}

?//以byte數組形式讀入類檔案
?byte[] readBytes(String filename) throws IOException
?{
??// A2
??File classfile = new File(filename);
??byte[] buf = new byte[(int) classfile.length()];
??FileInputStream in = new FileInputStream(classfile);
??in.read(buf);
??in.close();
??return buf;
?}

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.