Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-16-2008, 01:01 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
Rep Power: 0
gibsonrocker800 is on a distinguished road
Default 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.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 02-16-2008, 03:54 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 838
Rep Power: 4
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
Default
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)
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 02-16-2008, 04:03 AM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
Rep Power: 0
gibsonrocker800 is on a distinguished road
Default
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.
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 02-16-2008, 09:06 PM
Member
 
Join Date: Jan 2008
Location: South Africa
Posts: 18
Rep Power: 0
jimm1 has a little shameless behaviour in the past
Default
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 09:11 PM. Reason: Forgot to include Try-Catch block.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 02-16-2008, 10:49 PM
gibsonrocker800's Avatar
Senior Member
 
Join Date: Nov 2007
Location: New York
Posts: 143
Rep Power: 0
gibsonrocker800 is on a distinguished road
Default
Originally Posted by jimm1 View Post
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.
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?
__________________
//Haha javac, can't see me now, can ya?
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 05-12-2008, 09:24 AM
Eku Eku is offline
Senior Member
 
Join Date: May 2008
Location: Makati, Philippines
Posts: 234
Rep Power: 2
Eku is on a distinguished road
Default
when compiling within a program. You need to Specify the classpath where you would like to put the class. example, in invoking the:
Code:
Runtime.getRuntime().exec("javac foo.java");
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)
Code:
Runtime.getRuntime().exec("javac -classpath \"YourClassFolder\" \"Filepath\\foo.java\"");
Or if you want this: (putting the class in the same Folder)
Code:
Runtime.getRuntime().exec("javac -classpath \"FilePath\" \"Filepath\\foo.java\"");
^_^ God Bless in your code. I hope that helps.
Are you creating your own IDE? =)

Last edited by Eku; 05-12-2008 at 09:48 AM.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute an External Program through Java program Java Tip java.io 0 04-04-2008 03:40 PM
can you help me with my program please? java_fun2007 New To Java 0 12-21-2007 10:46 PM
cannot run the program amiey New To Java 1 11-20-2007 05:13 AM
How to execute an External Program through Java program JavaBean Java Tips 0 10-04-2007 10:33 PM
Why does this program not end? trill New To Java 1 08-07-2007 08:22 AM


All times are GMT +2. The time now is 09:37 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org