Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dynamically compile and execute java code from servlet

 
Yahya Elyasse
Ranch Hand
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
 
Yahya Elyasse
Ranch Hand
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Yahya Elyasse
Ranch Hand
Posts: 510
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic