AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



java-compiler: How to run a java file by a java program?

I want to compile and run a java file by another java file. Please give me source code.

java x 211
compiler x 5
Posted On : 2013-12-20 15:33:38.0
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939091
up-rate
5
down-rate

Answers


With the help of compiler API we can run or compile a java source code. I am giving you an example to compile a source code stored in a String and to compile and run a .java file. Let´s consider an example in which we have Test.java file to be compiled and run.

Test.java:
---------------

public class Test {
public static void main(String s1[]){
System.out.print("Hello Test");
}
}

--------------------------------------------------------------------------------------------------------------------------------------
Following is the java file which compile and run the above .java file and compile the source code stored in a String.
--------------------------------------------------------------------------------------------------------------------------------------

CompileString.java:
-------------------------------

import java.lang.reflect.Method;
import java.net.URI;
import java.util.Iterator;
import java.util.NoSuchElementException;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.ToolProvider;

public class CompileString {
public static void main(String[] args) throws Exception {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
String program = "class Test123{" + " public static void main (String [] args1){"
+ " System.out.println (\"Hello, World\");"
+ " System.out.println (args1.length);" + " }" + "}";

Iterable<? extends JavaFileObject> fileObjects;
fileObjects = getJavaSourceFromString(program);

compiler.getTask(null, null, null, null, null, fileObjects).call();

Class<?> clazz = Class.forName("Test");
Method m = clazz.getMethod("main", new Class[] { String[].class });
Object[] _args = new Object[] { new String[0] };
m.invoke(null, _args);
}

static Iterable<JavaSourceFromString> getJavaSourceFromString(String code) {
final JavaSourceFromString jsfs;
jsfs = new JavaSourceFromString("code", code);
return new Iterable<JavaSourceFromString>() {
public Iterator<JavaSourceFromString> iterator() {
return new Iterator<JavaSourceFromString>() {
boolean isNext = true;

public boolean hasNext() {
return isNext;
}

public JavaSourceFromString next() {
if (!isNext)
throw new NoSuchElementException();
isNext = false;
return jsfs;
}

public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
}

class JavaSourceFromString extends SimpleJavaFileObject {
final String code;

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;
}
}

--------------------------------------------------------------------------------------------------------------------------------------

Following is the example with Eclipse or MyEclipse IDE:
To download the example Click Here

Posted On : 2013-12-20 15:47:50
Satisfied : 1 Yes  0 No
profile Garima Gupta - anyforum.in Garima Gupta
596129558962
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in