2 Replies Latest reply on Oct 25, 2004 3:47 AM by panneer pandi

    is it possible to create .class file using a String

    panneer pandi Newbie

      Dear Experts,
      THe following class Test3 writing a "HelloWorld.class" file for the following program using javassist, without
      having a HelloWorld.java file.

      import java.io.*;
      import javassist.*;
      This program gnerates "HelloWorld.class " for the following java code
      public class HelloWorld{
      int abc;
      public HelloWorld(){
      public static void main(String a[]){
      HelloWorld h=new HelloWorld();

      public class Test3 {
      public static void main(String[] args) throws Exception {
      ClassPool pool = ClassPool.getDefault();
      CtClass cc = pool.makeClass("HelloWorld");
      CtField sf= CtField.make("int abc;",cc);
      String str="public HelloWorld(){abc=100;}";
      CtConstructor ccon=CtNewConstructor.make(str,cc);
      CtMethod m = CtNewMethod.make(
      "public static void main(String a[]){" +
      "HelloWorld h=new HelloWorld();"+
      "System.out.println(h.abc);" +
      "}", cc);
      cc. writeFile();


      My doubts are:
      1.In the above method , we are adding fields,constructor,methods seperatley
      CtClass cc = pool.makeClass("HelloWorld");

      2.my doubt is insted of adding each element(class,filed,constructor,method...) seperatly
      is the following is possible in javassist

      String str= "import java.io.*;"+
      " public class HelloWorld{"+
      "int abc;"+
      " public HelloWorld(){"+
      "public static void main(String a[]){"+
      "HelloWorld h=new HelloWorld();"+
      " System.out.println(h.abc);"+
      is it possible to create a "HelloWorld.class" using javassist by having the above String as a parameter

      3.When we are generateing class on the fly(like in the above example),how to include packages

      Thanks in Advance
      Jai Bharat Matha
      G.Panneer Pandi