GeeCON Prague 2014*
The moose likes Java in General and the fly likes Compile and create instance of a new java program from existing java program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Compile and create instance of a new java program from existing java program" Watch "Compile and create instance of a new java program from existing java program" New topic
Author

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

vikash talanki
Greenhorn

Joined: Oct 04, 2011
Posts: 4
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

Joined: Oct 27, 2005
Posts: 19697
    
  20

Check out URLClassLoader. You can use that to load the classes:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2383
    
  28

You might want to look at CGLib to generate bytecode directly at runtime.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7892
    
  21

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

    Joined: Oct 04, 2011
    Posts: 4
    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
     
    GeeCON Prague 2014
     
    subject: Compile and create instance of a new java program from existing java program