我将制作一个毕业项目,Web应用程序将编译java并运行代码。现在我可以编译和运行简单的java代码,如:
public class code{
public static void main(String[] args) {
System.out.println("Hello, World");
int a=2;
int b=3;
System.out.println(a*b);
}
}
İmages:
没关系,但是当代码包含扫描程序以从用户那里获取输入时,我通常无法从用户那里获取输入,因为我只是写输出。我不知道如何编译包含扫描器类的java代码。
FormHandlerServlet.java:
package com.crc64.servlet;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
@WebServlet("/formhandlerservlet")
public class FormHandlerServlet extends HttpServlet {
private String title;
private String docType;
@Override
public void init() throws ServletException {
this.title = "Using GET Method to Read Form Data";
this.docType = "<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">";
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.println(docType);
writer.println("<html>");
writer.println("<head>");
writer.println("<title>");
//writer.println(title);
writer.println("</title>");
writer.println("</head>");
writer.println("<body bgcolor=\"#f0f0f0\">");
writer.println("<h1 align=\"center\">");
//writer.println(title);
writer.println("</h1>");
writer.println("<ul>");
writer.println("<li>");
writer.println("<b>");
writer.print("Result:");
writer.println("</b>");
writer.println("<br>");
writer.println("<textarea style=\"width:500px;height:300px;\">");
// writer.println(request.getParameter("x"));
DosyayaYaz(request.getParameter("x"));
/*JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse");
CompilationResult result = compiler.compile(sources, new FileResourceReader(sourceDir), new FileResourceStore(targetDir));
System.out.println( result.getErrors().length + " errors");
System.out.println( result.getWarnings().length + " warnings");
*/
System.setProperty("java.home", "C:\\Program Files\\Java\\jdk1.8.0_45");
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList("C:\\Users\\ASUS\\workspace\\formservlet\\code.java"));
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, null,
null, compilationUnits);
boolean success = task.call();
fileManager.close();
int compilationResult = compiler.run(null, null, null, "C:\\Users\\ASUS\\workspace\\formservlet\\code.java");
//Runtime.getRuntime().exec("java code");
//Process process = Runtime.getRuntime().exec("code.class");
// Run("code","C:\\Users\\ASUS\\workspace\\formservlet");
String clasname="code";
String loc="C:\\Users\\ASUS\\workspace\\formservlet";
final String dosCommand = "cmd /c java -cp ";
final String classname = clasname;
final String location = loc;
try {
final Process process = Runtime.getRuntime().exec("cmd /c java -cp " + location + " " + classname);
final InputStream in = process.getInputStream();
final InputStream in2 = process.getErrorStream();
int ch, ch2;
while ((ch = in.read()) != -1) {
System.out.print((char) ch);
writer.print((char) ch);
}
while ((ch2 = in2.read()) != -1) {
System.out.print((char) ch2); // read error here
}
} catch (IOException e) {
e.printStackTrace();
}
writer.println("</textarea>");
/* writer.println("</li>");
writer.println("<li>");
writer.println("<b>");
writer.print("Last Name: ");
writer.println("</b>");
writer.println(request.getParameter("x"));
writer.println("</li>");*/
writer.println("</ul>");
writer.println("</body>");
writer.println("</html>");
}
public static void Run(String clasname,String loc){
}
private static void DosyayaYaz(String input) throws IOException{
String path = "C:\\Users\\ASUS\\workspace\\formservlet\\code.java";
FileWriter file = new FileWriter(path);
BufferedWriter writer = new BufferedWriter(file);
writer.write(input);
writer.close();
//System.out.println("Dosya Oluşturuldu");
}
private static final long serialVersionUID = 4991957348534606746L;
}
的index.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Get Method Sample</title>
</head>
<body>
<form action="formhandlerservlet" method="post">
<label for="Your Code">Your Code :</label><br>
<textarea for="x" type="text" name="x" style="width:500px;height:300px;"></textarea>
<input type="submit" value="Compile and Run" />
</form>
</body>
</html>
最后我应该怎么做才能在运行时使用Scanner Class?