Results 1 to 6 of 6
- 02-16-2008, 12:01 AM #1
Executing a program within a program
Hey guys. I am working on a project in which i need to execute javac inside. Basically, i want it so the user can enter java code, and then when they click a button, it compiles it. The thing is, i don't know how i can make it so that it executes javac. If anyone knows how to do this, please let me know.
- 02-16-2008, 02:54 AM #2
hey gibson. Most programming languages allow operating with child processes(ie: program within a program). Look into the class Runtime, specifically the exec method. I'm not sure how to apply javac, but you might find Runtime easier. Maybe... Runtime.exec("javac foo.java");
Vote for the new slogan to our beloved Java Forums! (closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice? Vote now!
Got a little Capt'n in you? (drink responsibly)
- 02-16-2008, 03:03 AM #3
Hm, well it's telling me that Runtime.exec can't be referenced from a static context, and i can now see that in the API because exec isnt a static method.
- 02-16-2008, 08:06 PM #4
Member
- Join Date
- Jan 2008
- Location
- South Africa
- Posts
- 18
- Rep Power
- 0
Do this:
try{
Runtime.getRuntime().exec("javac foo.java");
}
catch(IOException e) {
e.printStackTrace();
}
The method "getRuntime()" returns the Runtime object associated with the current java application.
And the try-catch catches any IO exceptions thrown.Last edited by jimm1; 02-16-2008 at 08:11 PM. Reason: Forgot to include Try-Catch block.
- 02-16-2008, 09:49 PM #5
Thanks man! Theres a problem though. It's not doing anything with javac or java... see like. I tried it out with like notepad and microsoft word and it executed them with no problem. Except, when i tried it out with javac Example.java ... It ran and compiled with no problem, but it didn't create the class file. I also tried it with like java Example.class and it didn't run it. Any solutions?
- 05-12-2008, 08:24 AM #6
Senior Member
- Join Date
- May 2008
- Location
- Makati, Philippines
- Posts
- 234
- Rep Power
- 6
when compiling within a program. You need to Specify the classpath where you would like to put the class. example, in invoking the:
This is true, the compiling takes place and the "java foo.java" or calling the notepad will surely run. But, you didnt specify where the class file should be located. You can add it by invoking the -classpath function in javac. It may look more like this: (Putting the class in a single folder)Java Code:Runtime.getRuntime().exec("javac foo.java");
Or if you want this: (putting the class in the same Folder)Java Code:Runtime.getRuntime().exec("javac -classpath \"YourClassFolder\" \"Filepath\\foo.java\"");
^_^ God Bless in your code. I hope that helps.Java Code:Runtime.getRuntime().exec("javac -classpath \"FilePath\" \"Filepath\\foo.java\"");
Are you creating your own IDE? =)Last edited by Eku; 05-12-2008 at 08:48 AM.
Similar Threads
-
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 02:40 PM -
can you help me with my program please?
By java_fun2007 in forum New To JavaReplies: 0Last Post: 12-21-2007, 09:46 PM -
cannot run the program
By amiey in forum New To JavaReplies: 1Last Post: 11-20-2007, 04:13 AM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 09:33 PM -
Why does this program not end?
By trill in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks