java - Accessing a newly compiled program mid run -
i'm using ecj genetic programming , have built takes best fit individual program after run, , creates java class function lisp code created.
i have program compile java file. there anyway can run newly compiled class file in same run?
i want able to:
runmain --> create java --> compile class --> call function in newly created class --> endmainrun
so far, i'm having trouble calling method in newly created class.
every time create new java file , compile, rewrites old one. whenever class called later, it's running old function pre-overwrite. tips appreciated!
edit: here's basic pseudo code show i've got far, lot of abstracted. assume there mathfunction.class file before run this.
pseudocode main(){ rungeneticprogrammingalgorithm(); generatejavafilefrombestfitindividual(name = mathfunction.java) //replaces old mathfunction.java compile(mathfunction.java) //using javacompilerapi, replaces old mathfunction.class double value = mathfunction.calculate(25);
the old function returned -1 value, new function should return 5, it's still returning -1. if put in loop, it'll keep spitting out -1, -1, -1....
edit 2:
i'm still having return same value despite function being different. here code:
url[] urls = null; file dir = new file("src" + java.io.file.separator + "ec"); url url = dir.touri().tourl(); urls = new url[] { url }; classloader cl = new urlclassloader(urls); class cls = cl.loadclass("ec.mathsolution"); mathsolution mathfunction = (mathsolution) cls.newinstance(); system.out.println(mathfunction.calculate(123.5));
edit 3:
found amazing source online here: http://www.toptal.com/java/java-wizardry-101-a-guide-to-java-class-reloading
achieving want non-trivial possible classloader magic.
the fact 'it's running old function pre-overwrite'... indicates creating instance of class same classloader got first time around.
i suggest reading on classloaders.
at high level algorithm should go:
- create java
- compile class
- create classloader child of current classloader.
- load class compiled @ step 2 using classloader
- instantiate class or otherwise use it.
- now see there new revision... start 1
i try edit answer code samples.
Comments
Post a Comment