Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: compiling a file

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default compiling a file

    suppose there is a file test.java .
    Aim is to compile this file and if compile is successful(no errors ) then run this file i.e java test.
    problem is i need to do all this i.e compilation and run of 'test' from a java program only.
    this java program must able to compile any file and then run that file.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: compiling a file

    Not sure if I understand what you are asking so I'll post a link to the intro tutorial (Trail: Getting Started (The Java™ Tutorials)), and if you want to do what I think lead you towards calling Runtime.getRuntime().exec(); to compile and run from your app (will be platform dependent though)

  3. #3
    Junior Member
    Join Date
    May 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: compiling a file

    To clear the thing..
    suppose there is a java program "runother".
    now when you run this program this would ask for input filename.
    and suppose the user input filename is "test".
    now the "runother" should able to compile the "test" and then run the "test" if there is no error in compilation stage.

  4. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: compiling a file

    this should do what you are wanting:

    String fileLocation;                                      // the folder the file is located
    String file;                                                   // the name of the actual file (without any extensions
                                                                       // eg "hello" not "hello.java"
     
    String[] commands = {"cmd", "cd" + fileLocation, "javac"+file+".java", "java"+file};
    Runtime.getRuntime().exec(commands);

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: compiling a file

    I think java has classes you can use to compile a program from a running java program.

Similar Threads

  1. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  2. Need help compiling java class files
    By peahead in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 11th, 2010, 09:04 AM
  3. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM
  4. Declaring variables in constructor and compiling
    By Newoor in forum Object Oriented Programming
    Replies: 3
    Last Post: December 5th, 2009, 01:07 PM
  5. Strange Compiling Error
    By crism85 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2009, 12:59 AM