java - JavaCompiler gives error when dependent class is being created -


i writed code generates 2 class write them buffer , compile them javacompiler. classes in .java files;

public class a{     public a() { }     public string tostring(){ return "a";}     } 

and

public class b extends arraylist<a> { public b() {     super(); }  public void additem(a a)  {     this.add(a); }  public void print() {     this.print();     } } 

something this.

however, name of classes randomly generated , when create file gives error this;

symbol:   class location: class b   

./src/a.java:4: error: cannot find symbol

(4th line "...extends arraylist..." , there ^ symbol under a)

my code generator compiles this;

first fill buffer template type classes compile this:

javacompiler compiler = toolprovider.getsystemjavacompiler(); compiler.run(null, null, null, f.getpath()); 

after create buffer , fill template b type classes compile this;

system.out.println(f.getparentfile().getpath()); compiler.run(null, null, null, f.getpath()); 

f is;

f = new file(("./src/" + name + ".java")); 

how can solve problem?

as mentioned in comment compiler need know class a when class b compiled. in example below add output directory compiled classes /tmp/bin/ classpath compiler in optionlist.

you either prevent create source files on filesystem, if don't need them such

public class compiledependent {      public static void main(string[] args) {         string sourceclassa = "public class {"                 + "  public string tostring() {"                 + "    return \"a\";"                 + "  }"                 + "}";         string sourceclassb = "import java.util.arraylist;"                 + "class b extends arraylist<a> {"                 + "  public void additem(a a) {"                 + "    this.add(a);"                 + "  }"                 + "}";          list<javafileobject> compilationunits = new arraylist<>();         compilationunits.add(new stringjavafileobject("a.java", sourceclassa));         compilationunits.add(new stringjavafileobject("b.java", sourceclassb));          list<string> optionlist = new arraylist<>();         // classpath current jvm + binary output directory         optionlist.add("-classpath");         optionlist.add(system.getproperty("java.class.path") + ":/tmp/bin");         // class output directory         optionlist.add("-d");         optionlist.add("/tmp/bin");          javacompiler compiler = toolprovider.getsystemjavacompiler();         standardjavafilemanager filemanager = compiler.getstandardfilemanager(                 null,                 locale.uk,                 charset.forname("utf-8")         );          boolean compiled = compiler.gettask(                 null,                 filemanager,                 null,                 optionlist,                 null,                 compilationunits).call();         system.out.println("compiled = " + compiled);     }      private static class stringjavafileobject extends simplejavafileobject {          final string code;          stringjavafileobject(string name, string code) {             super(uri.create("string:///" + name), kind.source);             this.code = code;         }          @override         public charsequence getcharcontent(boolean ignoreencodingerrors) {             return code;         }     } } 

or create java source files on file system. similar code above small change compilationunits. it's assumed files have been stored on given location.

list<file> sourcefiles = new arraylist<>(); sourcefiles.add(new file("/tmp/a.java")); sourcefiles.add(new file("/tmp/b.java")); 

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -