wood burning stoves*
The moose likes Servlets and the fly likes Dynamically compile and execute java code from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Dynamically compile and execute java code from servlet" Watch "Dynamically compile and execute java code from servlet" New topic
Author

Dynamically compile and execute java code from servlet

Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hi guys;

I'm trying to build the following web functionality :

using java servlets backed by a tomcat server container I want to build a java web application that given a peice of java code example it should in some way invoke the java compiler and compile the peice of java code reporting any compilation errors..and if compilation is successufull it should display the output of this java code.

Is there a way to invoke the javac compiler from within a servlet ?
can I get the output stream from invoking both the javac and java to interpret the code ?
should i use any library for dynamically interpreting java code?
what are your Ideas for solving this problem ?

thanks for your help indeed.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41598
    
  55
You can use the java.lang.Runtime.exec method to start external proceses (e.g. a "javac" process). That method returns a Process object, through which you can get access to the input, output and error streams of the newly created process. Some basics of Runtime.exec are explained here.


Ping & DNS - my free Android networking tools app
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Another option might be the Open Source compiler (JDT) that ships with Eclipse.
http://dev.eclipse.org/viewcvs/index.cgi/jdt-core-home/howto/batch%20compile/batchCompile.html?rev=1.4


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

thanks for replying...
for the suggestion of dimitter : invoking the javac from exec.RunTime() that require that the jdk should be installed and configured on the machine running the servlet. right ?
I'm still looking for any simple solutions.
thanks.
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Well, if you want to compile things, you will need a compiler! There are two main types of approach. You can use the JDK's own compiler. Or you can use another compiler, such as the JDT one mentioned. Which approach is best depends largely on what you can get access to; if you have access to a JDK, then invoking javac is an easy solution.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Yahya Elyasse
Ranch Hand

Joined: Jul 07, 2005
Posts: 510

Hi,
when I checked the eclipse JDT compiler i found this peice of code :



which dynamically compiles A.java ; my problem is how i can get the compilation errors -if any- to display them on my web page ? and also how can i know the compilation is successufull from above code ?
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

I would look at the API.
I'm sure there are java-centric ways to communicate with the compiler from your program.

The issues at hand here really aren't servlet specific and I think you're more likely to find people with experience with in process compiling in the Java In General (intermediate) forum.
I'll move this thread over there for you.
 
Consider Paul's rocket mass heater.
 
subject: Dynamically compile and execute java code from servlet