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 compile java source code by a java program?

I want to compile a java source code by another java program. Please guide me.

java x 211
compiler x 5
Posted On : 2013-12-20 14:06:54.0
profile Garima Gupta - anyforum.in Garima Gupta
596129558962
up-rate
5
down-rate

Answers


You can compile java source code by a java program using java compiler API. Let´s consider a simple example in which we store the java source code in a String and pass it as a parameter in a method to compile it.

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

package in.anyforum;

public class Test {
public static void main(String s2[]){
// TODO Auto-generated method stub
String sourceCode = "package in.anyforum;" +
"class anyforum{" +
"public static void main (String args[]){" +
"System.out.println (\"Hello, \");" +
"}" +
"}" ;
new CompilerAPITest().doCompilation("anyforum", sourceCode);
}
}
---------------------------------------------------------------------------------------------------------------------------------------
in doCompilation("anyforum", sourceCode) first parameter is the name of the class as defined on sourceCode String. Now look at CompilerAPITest.java class.
---------------------------------------------------------------------------------------------------------------------------------------


CompilerAPITest :
------------------------------

package in.anyforum;

import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Locale;
import java.util.logging.Logger;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

/**
* A test class to test dynamic compilation API.
*
*/
public class CompilerAPITest {
final Logger logger = Logger.getLogger(CompilerAPITest.class.getName()) ;


public void doCompilation (String name,String code){
/*Creating dynamic java source code file object*/
SimpleJavaFileObject fileObject = new DynamicJavaSourceCodeObject ("in.anyforum."+name,code) ;
JavaFileObject javaFileObjects[] = new JavaFileObject[]{fileObject} ;

/*Instantiating the java compiler*/
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

/**
* Retrieving the standard file manager from compiler object, which is used to provide
* basic building block for customizing how a compiler reads and writes to files.
*
* The same file manager can be reopened for another compiler task.
* Thus we reduce the overhead of scanning through file system and jar files each time
*/
StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(null, Locale.getDefault(), null);

/* Prepare a list of compilation units (java source code file objects) to input to compilation task*/
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(javaFileObjects);

/*Prepare any compilation options to be used during compilation*/
//In this example, we are asking the compiler to place the output files under bin folder.
String[] compileOptions = new String[]{"-d", "bin"} ;
Iterable<String> compilationOptionss = Arrays.asList(compileOptions);

/*Create a diagnostic controller, which holds the compilation problems*/
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();

/*Create a compilation task from compiler by passing in the required input objects prepared above*/
CompilationTask compilerTask = compiler.getTask(null, stdFileManager, diagnostics,null, null, compilationUnits) ;

//Perform the compilation by calling the call method on compilerTask object.
boolean status = compilerTask.call();

if (!status){//If compilation error occurs
/*Iterate through each compilation problem and print it*/
for (Diagnostic diagnostic : diagnostics.getDiagnostics()){
System.out.format("Error on line %d in %s", diagnostic.getLineNumber(), diagnostic);
}
}
try {
stdFileManager.close() ;//Close the file manager
} catch (IOException e) {
e.printStackTrace();
}
}


}

/**
* Creates a dynamic source code file object
*
* This is an example of how we can prepare a dynamic java source code for compilation.
* This class reads the java code from a string and prepares a JavaFileObject
*
*/
class DynamicJavaSourceCodeObject extends SimpleJavaFileObject{
private String qualifiedName ;
private String sourceCode ;

/**
* Converts the name to an URI, as that is the format expected by JavaFileObject
*
*
* @param fully qualified name given to the class file
* @param code the source code string
*/
protected DynamicJavaSourceCodeObject(String name, String code) {
super(URI.create("string:///" +name.replaceAll(".", "/") + Kind.SOURCE.extension), Kind.SOURCE);
this.qualifiedName = name ;
this.sourceCode = code ;
}

@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors)
throws IOException {
return sourceCode ;
}

public String getQualifiedName() {
return qualifiedName;
}

public void setQualifiedName(String qualifiedName) {
this.qualifiedName = qualifiedName;
}

public String getSourceCode() {
return sourceCode;
}

public void setSourceCode(String sourceCode) {
this.sourceCode = sourceCode;
}
}

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

It generates the .class file in your project folder. Following is the example with Eclipse or MyEclipse IDE:
To download the example Click Here

Posted On : 2013-12-20 15:04:18
Satisfied : 1 Yes  0 No
profile Saksham Kumar - anyforum.in Saksham Kumar
73433939091
Reply This Thread
up-rate
5
down-rate



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