Using java compiler API
I want to compile a java file
in jsp
. I created a html file and using its textArea
element I sent its text, which is expected to be code in java entered by user, to JSP on server and after collecting it in a string I made a file with .java
extension and wrote it with textArea
contents then i used compiler API but it is not compiling the file. I want to do something like this
index.html
<form action="formAction.jsp" method="post">
Please enter your text:
<br/>
<textarea name="textarea1" rows="5"></textarea>
<br/>
<input type="SUBMIT" value="Submit"/>
</form>
formAction.jsp
<%
String convert = request.getParameter("textarea1");
PrintWriter wr = response.getWriter();
File f =new File("file121.java");
if(!f.exists())
{
f.createNewFile();
wr.println("File is created\n");
}
FileWriter write = new FileWriter(f);
BufferedWriter bf = new BufferedWriter(write);
bf.write(convert);
bf.close();
wr.println("File writing done\n");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
int result = compiler.run(null,null,null,"file121.java");
if(result==0){
wr.println("Compilation Successful");
}
else{
wr.println("Compilation Failed");
}
%>