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 4 of 4

Thread: Java Based Compiling?

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    73
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Java Based Compiling?

    Hi guys! I was just wondering, is there any way to compile java code with java? If there is a simple enough way to do this, whether it be a library or native java code, I could really use it. I would also like it if I could compile a String which contains the source instead of a File if possible. I am looking for a simple way to do this, but it needs to be good. It cant be constantly giving errors when it shouldnt.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,738
    My Mood
    Mellow
    Thanks
    172
    Thanked 622 Times in 609 Posts

    Default Re: Java Based Compiling?

    What exactly are you trying to do?

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Java Based Compiling?

    Java is capable of invoking external processes, and it's capable of dynamic class loading.

    Theoretically you could create a .java file, invoke javac, and then dynamically load the resulting class file.

    Unloading a class is a problem, though. This means dynamically updating a class can't be done reliably (my main strife with garbage collection is there's no guarantees that an object will be reclaimed or when it will be, only that it can be).

  4. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Java Based Compiling?

    Hello.
    The only way to compile a java code is "javac" tool. And the syntax of this tool is that it always expects java code as a file.
    You can use built-in exec() method in your java program to compile other java programs dynamically. In this exec() method you have invoke the "javac" command. But how will you ensure if your java code has no syntax errors. This may be little bit tough. But you can still know that.
    Once you know class file is generated then you can load it dynamically using URLClassLoader class.
    You can write java code in strings. But ultimately you may atleast need to create a temp file in your program where you copy the string and then do the compilation.
    If compilation is successful then you can delete the temp files.

    Syed.

Similar Threads

  1. Java Based OS
    By sci4me in forum Java Theory & Questions
    Replies: 5
    Last Post: March 9th, 2013, 12:05 AM
  2. Compiling Java
    By TANKDS in forum Java Theory & Questions
    Replies: 6
    Last Post: February 6th, 2012, 01:05 PM
  3. Java based games..
    By imsuperman05 in forum Java Theory & Questions
    Replies: 1
    Last Post: January 12th, 2012, 03:09 PM
  4. Java compiling windows 7 problem
    By wiseone in forum The Cafe
    Replies: 3
    Last Post: July 26th, 2011, 03:06 AM
  5. Need help compiling java class files
    By peahead in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 11th, 2010, 08:04 AM