Java Dynamic Compilation

Source: Internet
Author: User

Java Dynamic compilation One, dynamic compilation introduction

The new creation object is a static load class, and it is necessary to load all classes that may be used at compile time.

100 classes, one of the classes is wrong and cannot be compiled.

The problem can be resolved by dynamically loading the class

Second, code example 2.1 Officebetter.java

Main interface

The dynamic compilation of class

Then invoke the instance and complete the dynamic compilation

1  Public classOfficebetter {2 3      Public Static voidMain (string[] args)throwsinstantiationexception, illegalaccessexception {4         Try {5             //dynamically loading classes, loading at run time6Class C =class.forname (args[0]);7             //create the class object through the class type8Officeable OA =(officeable) c.newinstance ();9 Oa.start ();Ten}Catch(ClassNotFoundException e) { One e.printstacktrace (); A         } -          -     }  the  -}

2.2 Officeable.java

An interface that is convenient for word when it is also convenient for Excel to use

1  Public Interface officeable {2      Public void start (); 3 }

2.3 Word.java

Implement the Officeable interface,

Dynamic compilation is in the wold of the time directly with the word class is good, Excel when the direct use of Excel is good

All of the original writing does not need to recompile, as long as the new features to run the good

1  Public class Implements officeable{23      Public void start () {4         System.out.println ("Word....starts ..."); 5     }6     7 }

Java Dynamic Compilation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.