i try to compile this java class
//package com.sun.tools.javac.v8;
import com.sun.tools.javac.v8.JavaCompiler;
import com.sun.tools.javac.v8.util.List;
import com.sun.tools.javac.v8.util.Context;
//import com.sun.tools.javac.v8.util.Convert;
import com.sun.tools.javac.v8.comp.Gen;
import com.sun.tools.javac.v8.comp.Env;
import com.sun.tools.javac.v8.tree.Tree;
//import javax.naming.Context;
import java.io.IOException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Hashtable;
public class Pmat1
{
static class Compiler
{
private ByteArrayOutputStream os;
public byte[] compile(String name, final String source)
throws Throwable
{
os= new ByteArrayOutputStream(1024);
final Context context= new Context();
JavaCompiler compiler= new JavaCompiler(context) {
public InputStream openSource(String filename) {
return new ByteArrayInputStream(source.getBytes());
}
void genCode(Env env, Tree.ClassDef classdef)
throws IOException
{
Gen.instance(context).genClass(env, classdef);
//writer.writeClassFile(os, classdef.sym);
}
};
List list= new List();
list= list.append(name +".java");
compiler.compile(list);
byte[] bytes= os.toByteArray();
os.close();
return bytes;
}
}
static class Loader
extends ClassLoader
{
private Hashtable mClasses= new Hashtable();
public void add(String name, byte[] bytes) { mClasses.put(name, bytes); }
public Class findClass(String name)
throws ClassNotFoundException
{
byte[] bytes= (byte[]) mClasses.get(name);
if (bytes == null)
return super.findClass(name);
return defineClass(name, bytes, 0, bytes.length);
}
}
public static void main(String[] argv)
throws Throwable
{
String source=
"public class fred {" +
" private int var= 128;" +
" public String toString() {" +
" return \"Var= \" +var;" +
" }" +
"}";
byte[] bytes= new Compiler().compile("fred", source);
Loader loader= new Loader();
loader.add("fred", bytes);
Class clazz= loader.findClass("fred");
System.err.println("Class: " +clazz.getName());
Object instance= clazz.newInstance();
System.err.println("Instance: " +instance);
}
}
It gives following error
java.lang.UnsupportedClassVersionError: com/sun/tools/javac/v8/JavaCompiler
(Unsupported major.minor version 48.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:486)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:111)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:248)
at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:297)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:286)
at java.lang.ClassLoader.loadClass(ClassLoader.java:253)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:313)
at test2.Pmat1.main(Pmat1.java:84)
Exception in thread "main"
How can i solve it.
Note: I try to change the major and minor numbers in the
com/sun/tools/javac/v8/JavaCompiler, but i couldn't find out where is those
numbers. Can some one tell me where those numbers also.