Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compile and create instance of a new java program from existing java program

 
vikash talanki
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a java program in eclipse to extract forms from lotusnotes db. Whenever I get a form I need to create a .java file containng a class with same name as of form. Then I have to compile this new .java class which gives me .class file & hence I can create an instance of that new class. I need to know whether this approach of creating,compiling & instantiating a new class is possible from a single existing java program.

My pseudocode goes below



But I am not able to get .class file for the new class. Can I instantiate the new class in the same program?? I was stuck at this point. Can any please help me

>
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out URLClassLoader. You can use that to load the classes:
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want to look at CGLib to generate bytecode directly at runtime.
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vikash talanki wrote:...I need to know whether this approach of creating,compiling & instantiating a new class is possible from a single existing java program.

The basic answer to your question is 'yes'. For the answer to your unasked (and possibly more important) question "is it a good idea?", we'd need to know a lot more about the requirements.

I guess my questions to you would be:
  • Why do you think you need to generate a program/class for each form?
  • What do these form classes do?
  • How do you plan on managing them?
  • Could your gazillion form classes be replaced by a single, or a few, slightly more complex classes, that use reflection?
  • Could you instead publish metadata that allows a single class to do the work of the reams you're banging out at the moment?

  • Without answers to those questions it's difficult to know what to advise, but your setup has a definite clunky feel to me.

    I saw a few projects that went down this road back in the 80's and they were, almost uniformly, disasters. Program-writing programs (or Class-writing classes) have their uses, but I suspect they still belong mostly to the purview of research on Turing-completeness or other such nerdy pastimes. They are also, because of their explosive nature, a b*tch to manage, and probably even worse to test.

    But I am not able to get .class file for the new class. Can I instantiate the new class in the same program?? I was stuck at this point. Can any please help me

    Sounds like you need to look at ClassLoader and it's offspring, but I think Rob's already steered you that way.

    Winston
     
    vikash talanki
    Greenhorn
    Posts: 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am running the similar program TestingRuntime from the command prompt and its creating both .java & .class files of RunTestTime.



    The output I am getting is :



    But When I am trying to run my original pgm(LotusNotesDBExtraction.java) & when I replace line 26 (I am running it from eclipse IDE)


    with


    I am getting the error as


    Where is the actual problem? in Runtime.getRuntime().exec() or in eclipse IDE.

    Please show me the way
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic