• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Generating / Compiling class at runtime

 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a String that contains definition of class. I want to compile it and execute method of it, without storing that String in a physical class file.

How to achieve this?
 
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've found the Javassist library very useful for creating classes in-memory, but I'm not sure if it accepts a complete class as input. You may have to break it down to methods.
 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://mindprod.com/jgloss/onthefly.html

Runtime compilation can be done as above. You could use a combination of MemorymappedIO, Reflection, Classloader to achieve execution.

Mustang has support for Compiler API.

Jeevan
[ November 17, 2006: Message edited by: Jeevan Philip ]
 
author and cow tipper
Posts: 5006
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You guys are blowing my mind. I had no idea this was possible.

-Cameron McKenzie
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JSP's are translated to servlets and then compiled to class files before it becomes usable.

And I do not think that tomcat(or any other compiler uses.) uses javac.exe for that..
There are classes available in tools.jar for compiliig java files at runtime.
 
Ulf Dittmer
Rancher
Posts: 43028
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And I do not think that tomcat(or any other compiler uses.) uses javac.exe for that..
There are classes available in tools.jar for compiliig java files at runtime.


Older versions of Tomcat used javac for this; these days it ships with the Eclipse Compiler (JDT). It did't use javac.exe, because that's just a command-line wrapper around the javac classes, but instead used the compiler classes directly (chiefly com.sun.tools.javac.Main).

But neither JDT nor javac work in-memory - both write .java and .class files to disk. You can find those in the work directory of your Tomcat installation.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can use it, Java 6 has a bunch of new APIs to access the compiler. See javax.tools package.
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everybody for your kind reply!
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic