How to use getInputStream() RunTime object
please help me to compile and run java files by using the following code snipet.
开发者_运维问答RunTime rt=RunTime.getRunTime();
Process p=rt.exec("//what to write here for compilation of java file");
then how will i catch the response by getInputStream().
please brief me.
thanks in advance
Actually, starting with Java 6, it's preferable to use the ToolProvider
api and use the Compiler programatically:
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
DiagnosticListener<? super JavaFileObject> listener =
new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager javaFileManager =
javac.getStandardFileManager(listener, Locale.getDefault(), charSet);
javaFileManager.setLocation(StandardLocation.SOURCE_PATH, sourcePaths));
javaFileManager.setLocation(StandardLocation.CLASS_PATH, classPath);
javaFileManager.setLocation(StandardLocation.CLASS_OUTPUT, outputPaths);
CompilationTask task = javac.getTask(
null, // no output writer
javaFileManager,
null, // no Diagnostic Listener
Arrays.asList("-source", "1.6", "-target", "1.6"),// source level Java 1.6
null, // no APT processors
sources
);
task.call();
And you can pass in a custom DiagnosticListener
if you want to process output.
You call javac.exe
to compile Java.
You should be using Process to execute it:
http://www.exampledepot.com/egs/java.lang/Exec.html
http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part3/Chapter23/runExtProg.html
精彩评论