Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

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
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
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
Sponsored Links
  #2 (permalink)  
Old 02-16-2008, 03:54 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 750
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
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");
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on July 13, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
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
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
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
jimm1 has a little shameless behaviour in the past
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
gibsonrocker800 is on a distinguished road
Send a message via AIM to gibsonrocker800
Quote:
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: 175
Eku is on a distinguished road
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
Sponsored Links
Reply


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

vB 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 +3. The time now is 04:25 PM.


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