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: Compile Java Source Files at Runtime in JRE

  1. #1
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    585
    Thanks
    0
    Thanked 106 Times in 91 Posts

    Default Compile Java Source Files at Runtime in JRE

    Hi.

    I am looking for a way to compile Java Source-Files at runtime and save them all in an executable jar; almost like an IDE would do.
    I know that there is the javax.tools package which provides a JavaCompiler interface and you can use ToolProvider.getSystemJavaCompiler() to get an instance of a compiler. However, this method has one important problem: it only works on machines that have the JDK installed. Not when only the JRE is installed.
    I guess at this point that I need some kind of third party library that offers an implementation of a JavaCompiler. Unfortunately, this is really complicated to search for on the internet since all top listings when searching "compile java at runtime jre" do not really provide a solution to the problem.

    But there must be a solution, right?

    Now you might be curious why I would need this and I am going to explain, just in case:
    I am writing a (somewhat) complex simulation software right now which is supposed to be used by people who have absolutely no knowledge of programming. At the same time, this software should provide the user with a certain amount of flexibility and control over the flow of the simulation.
    My previous take on this problem was to build a complex system to interprete user settings from a GUI. I would basically read the GUI input, output it to some kind of own scripting syntax which I just quickly made up and have that interpreted at runtime. Then I realized, that is a silly concept and I threw it out before I got far into the developement.
    The much better solution I came up with is taking the input from the GUI, create java source code from it and compile it at run-time. Seems much cleaner and nicer to me; will also probably have a better performance, but thats not really an issue anyways.

    Yes I know, I could also use some kind of existing scripting syntax like Ruby, Python, or whatever. But I already know java fairly well, and it is a java application, so why not stick to what I already know.


    So yeah, thank you very very much if you could point me in the right direction.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,659
    Thanks
    25
    Thanked 325 Times in 302 Posts

    Default Re: Compile Java Source Files at Runtime in JRE

    What sort of input is the user providing? Is the GUI not part of the simulation or something?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Compile Java Source Files at Runtime in JRE

    The teaching IDE DrJava does what you describe on a PC without the JDK installed using the "Eclipse Compiler." I tried to learn more about the Eclipse Compiler, how it differs from javac, how to get/use an instance of it, etc., and I didn't get very far quickly so gave up. It was an idle curiosity, and I wasn't idle long enough to keep at it. How the Eclipse Compiler obtains the source or class files that would normally come with the JDK is unknown to me. DrJava seems to have no trouble compiling code imported from across the entire SE, so it gets it somehow, but I'm not sure how.

  4. #4
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    585
    Thanks
    0
    Thanked 106 Times in 91 Posts

    Default Re: Compile Java Source Files at Runtime in JRE

    That does look promising indeed. Do you know of a link where I could download a standalone jar file of the JDT core package? Or would I need to build it manually from the source files?

    Edit: Nevermind, found a download and got it working:
    http://grepcode.com/snapshot/repo1.m...iler/ecj/3.7.1


    Here is a minimum working example:
    	public static void main(String[] args) {
    		String[] params = new String[] {
    			"-verbose",
    			"C:\\Project\\src\\Test.java",
    			"-d",
    			"E:\\Project\\bin"
    		};
     
    		BatchCompiler.compile(params, new PrintWriter(System.out), new PrintWriter(System.err), null);
    	}
    Compiles the java source file at C:\Project\src\Test.java and creates a class file at E:\Project\bin\Test.class.
    And yes, it works without having the SDK installed.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,132
    My Mood
    Mellow
    Thanks
    187
    Thanked 659 Times in 646 Posts

    Default Re: Compile Java Source Files at Runtime in JRE

    Thanks for the update. Good to know. I also found it on Maven, but the link you posted appears to be a later version.

Similar Threads

  1. Please help me! i am unable to compile and execute java files in command prompt
    By Sunanda Naik in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 24th, 2013, 08:42 AM
  2. how to compile your .java files
    By wilcomega in forum Java Theory & Questions
    Replies: 6
    Last Post: November 16th, 2011, 02:38 AM
  3. Length of Java source files?
    By ishtar in forum Java Theory & Questions
    Replies: 4
    Last Post: October 13th, 2011, 09:20 AM
  4. Can't compile .java file in JDeveloper due to missing files
    By javanewbie2 in forum Java Theory & Questions
    Replies: 4
    Last Post: July 7th, 2011, 10:22 AM
  5. [SOLVED] Compile a set of java files for a sudoku program
    By kanishk.dudeja in forum Java Theory & Questions
    Replies: 7
    Last Post: June 16th, 2011, 09:54 AM