import java.io.*;
import java.util.*;
import com.sun.tools.javac.main.Main;

public class javacException {
    String testclass="CLFtest.class";
    /* .jcod contents below */
    String hexString=
    "CAFEBABE0003002D001007000D0700060A000100040C000E000F01000D436F6E7374616" +
    "E7456616C7565010007434C467465737401000C434C46746573742E6A61766101000A45" +
    "7863657074696F6E7301000F4C696E654E756D6265725461626C6501000A536F7572636" +
    "546696C6501000E4C6F63616C5661726961626C6573010004436F64650100106A617661" +
    "2F6C616E672F4F626A6563740100063C696E69743E01000328295600010000000100000" +
    "00000000000";

    public static void main(String[] args) throws Exception {
        new  javacException().run();
    }

    public void run() throws IOException {
        writeTestFile(testclass);
        javac(testclass);
    }

    byte[] hexToByte(String str) {
        char[] CA = str.toCharArray();
        byte[] byteArry = new byte[str.length()/2];
        int bi = 0;
        for (int i = 0; i<CA.length ; i+=2) {
            char c1 = CA[i], c2=CA[i+1];
            byteArry[bi++] = (byte)((Character.digit((int)c1,16)<<4) +
                             Character.digit((int)c2,16));
        }
        return byteArry;
    }

    File writeTestFile(String classFileName) throws IOException {
        File f = new File(classFileName);
        FileOutputStream output  = new FileOutputStream(f);
        output.write(hexToByte(hexString));
        output.close();
        return f;
    }

    String javac(String className) {
        StringWriter sw = new StringWriter();
        PrintWriter out = new PrintWriter(sw);
        int rc = 0;
        
        List<String> javacArgs = new ArrayList<>();
        javacArgs.addAll(Arrays.asList(
            "-bootclasspath", System.getProperty("sun.boot.class.path"),
            "-d", ".", "-Xprint", "CLFtest"
        ));        
        rc = com.sun.tools.javac.Main.compile(
        javacArgs.toArray(new String[javacArgs.size()]),out);        
        out.close();
        if (rc > 1) {
            System.out.println(sw.toString());
            throw new Error("javac -Xprint failed. rc=" + rc);
        }
        return sw.toString();
    }
}


/* ******* CLFtest.jcod *********
class CLFtest {
  0xCAFEBABE;
  3; // minor version
  45; // version
  [] { // Constant Pool
    ; // first element is empty
    class #13; // #1
    class #6; // #2
    Method #1 #4; // #3
    NameAndType #14 #15; // #4
    Utf8 "ConstantValue"; // #5
    Utf8 "CLFtest"; // #6
    Utf8 "CLFtest.java"; // #7
    Utf8 "Exceptions"; // #8
    Utf8 "LineNumberTable"; // #9
    Utf8 "SourceFile"; // #10
    Utf8 "LocalVariables"; // #11
    Utf8 "Code"; // #12
    Utf8 "java/lang/Object"; // #13
    Utf8 "<init>"; // #14
    Utf8 "()V"; // #15
  } // Constant Pool

  0x0001; // access 
// right:
//  #2;// this_cpx
// end right
// wrong:
  #0;// this_cpx
// end wrong
  #1;// super_cpx

  [] { // Interfaces
  } // Interfaces

  [] { // fields
  } // fields

  [] { // methods
  } // methods

  [] {  // Attributes
  } // Attributes

} // end class CLFtest


*/
