Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java API
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
Other JSE/JEE APIs
analog javascript's eval function
Jack Leeyohn
Greenhorn
Posts: 2
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi.
when i run the following program,it will generate a Temp.class file, how should i do in order not generate Temp.class file.
please help me,thanks in advance.
MyClassLoader.java
import java.util.Arrays; import javax.tools.SimpleJavaFileObject; import javax.tools.JavaFileObject; import javax.tools.JavaCompiler; import javax.tools.ToolProvider; import javax.tools.DiagnosticCollector; import java.net.URI; public class MyClassLoader extends ClassLoader { @Override public Class<?> findClass(String str) throws ClassNotFoundException { JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); //the source code stored in an inherited class from the JavaFileObject JavaFileObject file = new JavaSourceFromString("Temp", str.toString()); Iterable compilationUnits = Arrays.asList(file); //build a compiler task JavaCompiler.CompilationTask task = compiler.getTask(null, null, null, null, null, compilationUnits); //compile source code boolean result = task.call(); //success if (result) { //load the class file from memory return loadClass("Temp"); } return null; } } class JavaSourceFromString extends SimpleJavaFileObject { private String name; private String code; public JavaSourceFromString(String name, String code) { super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE); this.code = code; } public CharSequence getCharContent(boolean ignoreEncodingErrors) { return code; } }
Eval.java
import java.lang.reflect.Method; public class Eval { public static Object eval(String str) throws Exception { StringBuffer sb = new StringBuffer(); sb.append("public class Temp"); sb.append("{"); sb.append(" public Object getObject()"); sb.append(" {"); sb.append(" " + str + "return new Object();"); sb.append(" }"); sb.append("}"); Class clazz = new MyClassLoader().findClass(sb.toString()); Method method = clazz.getMethod("getObject"); //call the method through the reflection return method.invoke(clazz.newInstance()); } public static void main(String[] args) throws Exception { Object rval = eval("System.out.println(\"Hello World\");"); System.out.println(rval); } }
Ulf Dittmer
Rancher
Posts: 43081
77
posted 14 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
That's what the
Java
compiler does - create class files from source code. If you don't want that, you need to use some other approach for generating the class, like Javassist:
http://www.javaranch.com/journal/200711/creating_java_classes_runtime_expression_evaluation.html
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Reflecting classes not in classpath
EJB PK class
Fake a post request from java class
How i read,remove html script tags,content ?
redirect .class files programatically
More...