• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Dynamically compile and execute java code from servlet

 
Ranch Hand
Posts: 510
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Yahya Elyasse
Ranch Hand
Posts: 510
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic