大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> Script -> 动态编译Java程序

动态编译Java程序

时间: 2021-07-31 作者:daque

在sun jdk 1.2及后续本子中,包括了一组可在步调运转功夫编写翻译和实行java代码的api。那些api被包括在tools.jar类库中。这个功效承诺java步调在运转时动静编写翻译、实行小的代码块,在有些情景下这个功效会让java运用步调的框架结构越发精巧、盛开。正文假设读者群仍旧在计划机中安置并摆设好了sun jdk 1.2或更高的本子,并对javac编写翻译器吩咐有所领会。在java步调中运用编写翻译器假设要运用javac吩咐编写翻译 /home/mytest目次下test.java文献,并设定class文献寄存在/home/mytest/classes路途下,输出底下吩咐:javac -d /home/mytest/classes test.java到达同样的手段,也不妨运用sun供给的一个java编写翻译器的api来实行。它的运用也很大略,中心代码段如次:…string[] args = new string[] {“-d”, “/home/mytest/classes”, “test.java”};int status = javac.compile(args);…javac编写翻译东西被安置在jdk根目次的/bin目次下,控制将源代码编写翻译成运转于jvm的字节码。究竟上,咱们常常运用/bin目次下的javac编写翻译东西来编写翻译java源文献。即使在java步调中动静编写翻译大肆拟订的java语句,运用这个外部的javac编写翻译器就显得不够精巧了。固然偶尔可运用runtime类来实行一个外部吩咐,但即使想领会代码能否被编写翻译经过、编写翻译时爆发了什么缺点,用runtime类的exec()本领就很难实行了。在sun的jdk 1.2及后续本子中,jdk安置路途的/lib路途下包括了一个tools.jar文献,这个类库包括了一个完备的编写翻译器包。com.sun.tools.javac.main是编写翻译器的主类进口,即使仍旧熟习了javac编写翻译器吩咐行的运用本领,很简单领会这个类的运用本领。本领compile(string[] p)实行编写翻译举措,参数p是一个string数组,用来寄存javac吩咐的参数选项,编写翻译后的状况归来一个int值,其对应值参考如次表所示:表 状况参数与对应值exit_ok 0exit_error 1exit_cmderr 2exit_syserr 3exit_abnormal 4在步调实行时编写翻译和实行java语句从上头一段中,咱们仍旧基础领会了动静编写翻译一个java文献的本领。那么,怎样运转时动静编写翻译指定的java语句呢?这边须要一个本领。假如要动静编写翻译的java条语句如次:system.out.println(“hello,this runtime code!”);编写翻译器不扶助编写翻译单个java语句,被编写翻译的东西必需是一个以.java为后缀的、构造正当的类源步调文献,以是须要对这个语句举行变革,形成一个完备的类,并把这条语句置入main本领中,便于尝试。public class <偶尔类文献名> {public static void main(string[] args) throws exception {system.out.println(“hello,this runtime code!”);}}如许,欲动静编写翻译的代码仍旧被步调动静组装成了上头那段代码,筹备处事还没有中断,然而看上去处事在趋势略微的搀杂化。由于上述代码暂时还寄存在外存中,编写翻译器犹如对一个硬盘文献更感爱好。咱们须要援用java.io.file类(jdk 1.2之上),创造一个偶尔的文献来寄存上述代码的实质。java.io.file类的静态本领createtempfile()本领保护所创造的文献名是不反复的,如许会增大这段步调的精巧性。精巧性在于于真实运用到体例框架结构中的战略。system.getproperty(“user.dir”)用来赢得暂时路途,在这边动作偶尔文献的寄存目次。file file;file = file.createtempfile(“javaruntime”, “.java”, new file(system.getproperty(“user.dir”)));string filename = file.getname();string classname = getclassname(filename);//将代码输入到文献printwriter out = new printwriter(new fileoutputstream(file));out.println(“public class” + classname + “ {”};out.println(“..代码..”);out.println(“}”);//封闭文献流out.flush();out.close();咱们商定被创造的偶尔文献名以“javaruntime”为头缀(可大肆定名),后缀名以“.java”结果。一个待编写翻译的java源文献已被迫态天生。下一步要从com.sun.tools.javac包中创造一个main范例,挪用javac.compile()本领编写翻译这个偶尔文献:private static com.sun.tools.javac.main javac = new com.sun.tools.javac.main();string[] args = new string[] {“-d”, system.getproperty(“user.dir”),filename };int status = javac.compile(args);假设偶尔文献经过了编写翻译器文法考证等考证,编写翻译胜利(status值即是0,参看前表),在当出息序的运转目次下就会多了一个java类文献。咱们将经过实行这个java 类文献,来模仿实行欲动静编写翻译代码的截止。java供给在运转功夫加载类的个性,可动静辨别和挪用类结构本领、类字段和类本领。java.lang.reflect.method实行了member接口,不妨挪用接口的本领来赢得本领类的称呼、修遁辞等。本领getruturntype()、getparametertypes()、getexeptiontypess()等归来被表白本领的结构消息。method另一个要害的个性是不妨挪用invoke()实行这个本领(精细运用本领不妨察看java.lang.reflect包文书档案)。底下这段代码中创造一个java.lang.reflect.method类本领,挪用getmethod()本领赢得被组装的main本领的映照,这段代码如次:try {// 考察这个类class cls = class.forname(classname);//挪用main本领method main = cls.getmethod(“main”, new class[] { string[].class });main.invoke(null, new object[] { new string[0] });}catch (securityexception se) {debug(“access to the information is denied:” + se.tostring());}catch (nosuchmethodexception nme) {debug(“a matching method is not found or if then name is or :” + nme.tostring());}catch (invocationtargetexception ite) {debug(“exception in main: ” + ite.gettargetexception());}catch (exception e){debug(e.tostring());}运转截止参如次:hello,this runtime code!演示步调底下给出了一个大略的java步调,这个步调说领会怎样运用sun的javac编写翻译器实行动静编写翻译java语句。运转该步调须要计划机安置jdk 1.2之上本子,并在classpath中或运转时指定tools.jar文献场所。步调构造:◆ compile() 编写翻译java代码,归来天生的偶尔文献;◆ run()运转编写翻译的class文献;◆ debug()输入调节和测试消息;◆ getclassname()从一个java源文献赢得类名;◆ readline()从遏制台读取用户输出的java code。import java.io.file;…public class runtimecode{/**编写翻译器*/private static com.sun.tools.javac.main javac = new com.sun.tools.javac.main();/**等候用户输出javacode,而后编写翻译、实行*/public static void main(string[] args) throws exception{…run(compile(code));}/**编写翻译javacode,归来偶尔文献东西*/private synchronized static file compile(string code)throws ioexception,exception { file file;//在用户暂时文献目次创造一个偶尔代码文献file = file.createtempfile(“javaruntime”, “.java”,new file(system.getproperty(“user.dir”)));//当假造机退出时,简略此偶尔java源文献file.deleteonexit();//赢得文献名和类名字string filename = file.getname();string classname = getclassname(filename);//将代码输入到文献printwriter out = new printwriter(new fileoutputstream(file));out.println(“/**”);…//封闭文献流out.flush();out.close();//编写翻译代码文献string[] args = new string[] {“-d”, system.getproperty(“user.dir”),filename };//归来编写翻译的状况代码int status = javac.compile(args);//处置编写翻译状况…}/**实行方才编写翻译的类文献*/private static synchronized void run(file file)…//当假造机退出时,简略此偶尔编写翻译的类文献 new file(file.getparent(), classname + “.class”).deleteonexit();try {// 考察这个类class cls = class.forname(classname);//映照main本领method main = cls.getmethod(“main”, new class[] { string[].class });//实行main本领main.invoke(null, new object[] { new string[0] });}catch (securityexception se) {…}}/**打字与印刷调节和测试消息*/private static void debug(string msg) {system.err.println(msg);}/**按照一个java源文献名赢得类名*/private static string getclassname(string filename){return filename.substring(0,filename.length()-5);}/**从遏制台赢得用户输出的java代码段*/…}编写翻译运转上述代码,在please input java code提醒下输出以次代码:for(int i=0;i<10;i++){system.out.println(“this is:”+i);}运转截止如次所示:please input java code:for(int i=0;i<10;i++){system.out.println(“this is:”+i);}wait....--------------------this is:0this is:1this is:2this is:3this is:4this is:5this is:6this is:7this is:8this is:9归纳在中小型企业运用体例平台中,运用代码动静编写翻译本领贯串oo编制程序模子,可在体例不菪机前提下保护体例的可扩充性和舒卷性。即使你是一个java步调员,稍加安排之上代码,还不妨扶助调节和测试小段的java代码。

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.