• Post Reply Bookmark Topic Watch Topic
  • New Topic

automatically compiling a java program  RSS feed

 
shankar surendranath
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i am experimenting with a java program that automatically recompiles and runs another program if it is modified.if the other program is not modified then my program will simply run it. i am running this on a windows 2000 machine. I am using File object for checking if the source code of the program is modified. if it is modified then a Runtime instance will call the java.exe or javac.exe as the need maybe. Please tell me what arguments i have to pass to the runtime instance.thanks in advance.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should be exactly what you'd type in a command window to run the program or do the compile manually. If you have to do two or three commands - like setting up path and classpath before running java or javac - consider putting the tricky bits in a batch file and launching that from your runtime.exec().
 
shankar surendranath
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried it with runtime instance.here is the code.runtime is an instance of the Runtime.
// compiling the program
runtime.exec("javac Myprogram.java");
// running the program
runtime.exec("java Myprogram.java");
But it doesnt work.can you suggest a solution for this?
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If nothing else, this will likely fail because the java command launches before the javac command is done. When you run a command you get back a Process object. Look at the Process API for a way to wait for it to finish and check the return code before launching the next one.
You can also get the stdout and errout streams from the process and read them to see what's going on. These should help you figure out just what's going wrong. You might display the output on the screen, or scan it for interesting success or failure messages from the compile. If you are reading these two streams until you get an EOF (-1) byte I think you can skip the waitFor() call, because the streams usually end (in my limited tests) long after the waitFor() returns.
As another approach, you might exec a batch file which runs javac and java and redirects the outputs to files. Then you can read the files manually to see what's going wrong. Hope that helps!
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!