Compiling a Java file with code from a Java file

I am currently creating a personal (possibly open source) java terminal. I want to create a command that will create + compile a Java file when executed, except that I am not too sure how to actually do it. Is it possible? Or am I just dreaming?

0


source to share


3 answers


You can also use Groovy , which is great if you just want to compile and run a line or two of Java code from within your application. The application can be in regular Java, and Groovy is only used to compile dynamically generated code. Whichever solution you choose, be careful as performing user input as the code can lead to security issues (injection vulnerability).



+1


source


compile java file



See STBC . It uses JavaCompiler

to compile the code in the text area.

+1


source


I agree with @eee's comment, which javax.script

is probably very suitable for your project, script code is easier to deal with than Java code. I've used it successfully in the past for plugin APIs, I don't remember having trouble getting it up and running.

Most projects I know about compiling real Java at runtime use the Eclipse compiler . Java 6 javac

can be accessed entirely programmatically. I've never used any of these myself. These two and some other compilers can be accessed through the Commons-JCI if needed.

0


source







All Articles