permaculture playing cards*
The moose likes Java in General and the fly likes Can i build and compile a JAVA pgm in another JAVA program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can i build and compile a JAVA pgm in another JAVA program?" Watch "Can i build and compile a JAVA pgm in another JAVA program?" New topic
Author

Can i build and compile a JAVA pgm in another JAVA program?

bob connolly
Ranch Hand

Joined: Mar 10, 2004
Posts: 204
Hello!

I'm wondering if i can build a JAVA program 2 within JAVA a program 1, compile the program 2 in the program 1 and then run program 2 from the program 1?

I need to do this because the PipedInput/OutputStream processes need to know which, in this case Oracle table, is being used, to verify both ends of the pipe, right up front!

I've tried every combination of things, and the only one that works is when everything the program needs is known by the system right up front!

And because i have over 300 potential files to process, for me to create a unique program for every file, would result in having to create over 300x4 unique programs!

Right now i am extracting the file requested, passing the parms to a .ksh shell, the .ksh shell assembles the pieces of the program, including table name seleted, compiles the program and then runs the program!

So now i'd like to see if i can put this process into a java program as some of our users are not using UNIX!

Thanks very much for any ideas!

bc
K Riaz
Ranch Hand

Joined: Jan 08, 2005
Posts: 375
Use the Reflection API
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Reflection won't really help you compile and run a Java program; it only can be used to inspect and access existing compiled Java classes. It will be necessary to use reflection (in the form of the Class.forName() and Class.newInstance() methods) to load the code after you've compiled it.

You can invoke Sun's Java compiler like this, or you can use the Runtime.exec() method to run it via the command-line interface.

Although they've always discouraged it in the past, I think there may even be an official Sun document somewhere about invoking the newest version of the compiler from code -- I just can't remember where I saw it.


[Jess in Action][AskingGoodQuestions]
bob connolly
Ranch Hand

Joined: Mar 10, 2004
Posts: 204
Thanks again Ernest!!!

I think this 'java statement' approach will do the trick and save lots of headaches! thanks also Kashif!

Ernest, i have your book 'Jess in Action' right over my desk and follow your emails, can't wait to use Jess one of these days!

Have a great week!

bc
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by bob connolly:
I need to do this because the PipedInput/OutputStream processes need to know which, in this case Oracle table, is being used, to verify both ends of the pipe, right up front!


I don't fully understand what you are getting at here, but I strongly suspect that there will be a *much* simpler approach to solve this problem. Care to elaborate?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
bob connolly
Ranch Hand

Joined: Mar 10, 2004
Posts: 204
Sure Ilja!

Basically, i was trying to get the following PipedInput/OutputStream program to work, by passing an oracle table name into the program, but for some reason, the pipes don't want to connnect when the table_name is passed into the program, but if the name is "hard coded" in the program it works fine!

So what i've done so far, is build the program via UNIX scripts, ie, when a user selects a file from an online screen, the script concatenates the program pieces together ie, header_code + oracle_table_name + trailer_code, compiles it and runs it!

But now i need to package up the process so that it runs via JAVA only components as 1 of our users isn't using UNIX, so i've been able to put most of processes into 2 JAVA programs, except for this one, which extracts XML from an oracle table and validates it using a SAX validator and instead of writing the XML to an external file, it buffers the XML directly into the validation routine, eliminating a huge external XML file!

This feature was "supposed" to be available in 10g!

Here is the program if you might have any ideas about why the Piped files don't want to connect unless the table_name is hard coded, see <<<<<<<<<<<
in the code

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Is this whole effort to change the table name? Can you just get table names from parameters or a file?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by bob connolly:
Basically, i was trying to get the following PipedInput/OutputStream program to work, by passing an oracle table name into the program, but for some reason, the pipes don't want to connnect when the table_name is passed into the program, but if the name is "hard coded" in the program it works fine!


Can you show us how you tried to pass the table_name into the program? I'm convinced that we should be able to get it work.
bob connolly
Ranch Hand

Joined: Mar 10, 2004
Posts: 204
Well, actually, i am passing the table name into the program in:

this.TABLE_NAME =con.table.toUpperCase();
this.table_name =con.table.toLowerCase();

but if i didn't have that table name hard coded initially in this line of code:

public static final String IN_TABLE=new String("fn_ll_lossmit"); <<<<<<<

, the pipes don't seem to want to make the connections, must have tested this at least 10 times!

But i will re-test it again this morning just to be triply sure!

Thanks!
bc
bob connolly
Ranch Hand

Joined: Mar 10, 2004
Posts: 204
Hi Ilja!

Thanks for the prompt to go back and look at this from the top! as it seems to be working fine by passing the table_name in, great news! and i'm now trying to figure out what got me on this track to begin with!

So appreciate everyone's assistance on this and i quess the moral of this distraction is, the only way to be an optimist in this business is to be a pessimist, when the analyst, programmer and tester are one in the same?

But on the bright side, i now know how to build, compile and run a JAVA program from within another JAVA program, very nice! and i wrote a Groovy script to do a similar thing!

Have a great week all!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can i build and compile a JAVA pgm in another JAVA program?
 
Similar Threads
Setting Java Path in Linux
Data Access Object Design Questions
shell and getRuntime
Need a best messaging Solution
problem with execution of script