Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generating .class file for JVM

 
Raghav Bali
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manuel Moons
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghav Bali
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic