This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
I am working on a project that requires me to generate a java ".class" file on the go that can be later on compiled on the JVM. After learning and working with MSIL (Microsoft IL) which is also a stack based intermediate programming language, the following are the problems I am facing :
1. As compared to the IL (for C# or VB) the java bytecode in ".class" file contains information in a structured manner and as far as i could understand the file, it contains the metadata apart from the program data, is it true?? Can i generate the same in a template form for every class file??
2. Is it compulsory to generate the class file in binary??
I have refered to " Programming for the Java™ Virtual Machine By Joshua Engel " but it has not served my purpose as I have already learned about the JVm instruction set.
Can anybody please help me with this?? All help will be highly appreciated.
An example for generating a simple class file would be really helpful as i could not locate a single 1 yet.
Raghav Bali wrote:... requires me to generate a java ".class" file on the go that can be later on compiled on the JVM.
Your terminology is not precise. Class files don't get "compiled on the JVM". Normally, you start with a Java source file, you compile that with a Java compiler into a .class file, and you run that class file on a JVM (Java Virtual Machine).
So, you need to write a program that generates class files on the fly?
The specification for the Java class file format can be found in The Java Language Specification. Yes, class files do contain header information and metadata in addition to the bytecode. Class files have a very specific layout, and they are not plain text files.
You most likely don't want to generate class files completely from scratch. There are a number of Java libraries available for working with byte code and generating class files. For example Apache BCEL and ASM.
Thank you all for prompt replies.
My apologies for the wrong terminology used.
The problem is i am using C#.NET and my application is aimed to generate bytecode that can be run on the JVM . My application takes input in a normal plain text form (not in java synatx) so my need is to generate the bytecode from scratch. "Converting that plain text to java syntax will not feasible"
I more thing, BECL allow manipulations to the bytecode or even help in generating it but in this case i am not having a standard syntax that can be given input to BECL or ASM.