File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Generating .class file for JVM Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Generating .class file for JVM" Watch "Generating .class file for JVM" New topic

Generating .class file for JVM

Raghav Bali

Joined: Nov 25, 2009
Posts: 2
Hello Everybody,
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.

-computers are d best thing yet to happen!!!
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
I'm not quite sure what you need but can't you just generate a java source file and compile it to a .class file?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15093

Welcome to JavaRanch.

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Raghav Bali

Joined: Nov 25, 2009
Posts: 2
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.
I agree. Here's the link:
subject: Generating .class file for JVM
It's not a secret anymore!