Results 1 to 6 of 6
  1. #1
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    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.

  2. #2
    CaptainMorgan's Avatar
    CaptainMorgan is offline Moderator
    Join Date
    Dec 2007
    Location
    NewEngland, US
    Posts
    835
    Rep Power
    8

    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)

  3. #3
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    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.

  4. #4
    jimm1 is offline Member
    Join Date
    Jan 2008
    Location
    South Africa
    Posts
    18
    Rep Power
    0

    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.

  5. #5
    gibsonrocker800's Avatar
    gibsonrocker800 is offline Senior Member
    Join Date
    Nov 2007
    Location
    New York
    Posts
    143
    Rep Power
    0

    Default

    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?

  6. #6
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    when compiling within a program. You need to Specify the classpath where you would like to put the class. example, in invoking the:
    Java 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)
    Java Code:
    Runtime.getRuntime().exec("javac -classpath \"YourClassFolder\" \"Filepath\\foo.java\"");
    Or if you want this: (putting the class in the same Folder)
    Java 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.

Similar Threads

  1. Replies: 0
    Last Post: 04-04-2008, 03:40 PM
  2. can you help me with my program please?
    By java_fun2007 in forum New To Java
    Replies: 0
    Last Post: 12-21-2007, 10:46 PM
  3. cannot run the program
    By amiey in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 05:13 AM
  4. Replies: 0
    Last Post: 10-04-2007, 10:33 PM
  5. Why does this program not end?
    By trill in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 08:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •