• 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

New article: Creating Java classes at runtime for evaluating numerical expressions

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The November issue of the JavaRanch Journal has an article by yours truely about Creating Java classes at runtime for evaluating numerical expressions. You can check it out here, and if you have comments or question, feel free to discuss them in this thread.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very cool! I had no idea Javassist was so easy to get started with.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great article. I have one question regarding this article. You mentioned at the end of this article that there are Low Level API to do the same thing. Can you tell me how can i create classes on runtime without using Javaassist ?

Regards

Owais
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javassist's low-level APIs can be used to create class files if you know your way around JVM bytecode (I've yet to meet anyone who does, but the API makes it possible).

What requirement prompts you to ask for a way to create classes without using Javassist?
 
Owais Zahid
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just read your article and out of curiosity asked this question. However, we may need to use low level API's in a condition where adding new library file (like Javaassist) is not permitted. This unsually happens when you are working on a large project and management will not allow any third-party libs.

Secondly, can we do this by having custom implementation of Class Loader ?
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java 6 has an API for working with the compiler. If you save the source code for the complete class (and give it a unique name) then you can create actual class files at runtime. If the class implements some specific interface, you can access it after compilation through something like

MyInterface obj = (MyInterface) Class.forName("MyUniqueClassName").newInstance()

[ UD: fixed broken URL ]
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I actually read that article that you have posted but I was trying to execute the examples on Netbeans but I am getting certain runtime errors. Please do have a look.

Exception in thread "main" java.lang.NoClassDefFoundError: Evaluator
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.lang.ClassLoader.defineClass(ClassLoader.java:465)
at javassist.ClassPool$LocalClassLoader.loadClass(ClassPool.java:353)
at javassist.ClassPool.writeAsClass(ClassPool.java:404)
at javassist.CtClass.toClass(CtClass.java:755)
at assist.Example2.main(Example2.java:23)
Caused by: java.lang.ClassNotFoundException: Evaluator
at java.lang.ClassLoader.findClass(ClassLoader.java:358)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 8 more
Java Result: 1

Please do help asap. I need to use this concept in a tool that I am building.
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not familiar with NetBeans, but you need to make sure that all required classes (including Evaluator) are in its classpath when it tries to run the code.

It may be easier to get this to run on the command line using something like:

java -classpath javassist-3.4.jar;. Example1

(assuming you're on Windows, otherwise use : instead of ; )
 
Ankit Luthra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can this functionality be used to read code from text files and make executable java classes at run time because that is what I'm looking to do..
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Javassist can be used to compile arbitrary Java code; it doesn't matter what the code does.
 
Ankit Luthra
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all the help..
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the great article, really nice code that works like a charm. I use it for genetic programming, and already integrated Example1 successfully. Seems to calculate complex equations faster than most other interpreters I've tested! However, I have problems when trying to intergrate Example2 to my project. It seems that it just won't compile for me outside the standard package:

Exception in thread "main" javassist.CannotCompileException: by java.lang.NoClassDefFoundError: Evaluator
at javassist.ClassPool.toClass(ClassPool.java:1099)
at javassist.ClassPool.toClass(ClassPool.java:1042)
at javassist.ClassPool.toClass(ClassPool.java:1000)
at javassist.CtClass.toClass(CtClass.java:1140)
at tiny.Example2.main(Example2.java:25)
Caused by: java.lang.NoClassDefFoundError: Evaluator
at java.lang.ClassLoader.defineClass1(Native Method)
.....


What I did:
1. set up a new project with only the two files Evaluator.java and Example2.java in the default package, set classpath to javassist, and it works fine
2. used the same files (added only package definitions) in an existing project - and I get the error message. Build path to javassist exists.

Help would be highly appreciated, this is driving me crazy for one day now...

Working on Windows 7 with Eclipse by the way. Thanks in advance!
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That seems more like an Eclipse question (which I don't use, either, so I can't help with that). Maybe try our Eclipse forum.
 
Enrico Palazzo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ulf! Is there maybe a smart workaround, something like defining the Evaluator Interface in the Example2 class?
 
Enrico Palazzo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Me again, solved my issue. I'm posting it in case someone else runs into the same problem. The solution is quite straightforward, a shame that I didn't see it for so long. If you use the Example2 code in a package (as one should do), one must refer to the full qualified name when referring to Evaluator. Assuming that you use all files in a package called "tes", the according command is:

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An approach to avoid the above described java.lang.NoClassDefFoundError:
When referring to an existing class or package get the meta information from a textual reference to this class directly instead of using String literals.



Another advantage is that refactorings (rename or move to another package) of the referenced class is applied to the class name (and import declarations if needed) in the code automatically.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic