Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
");

        }
        FileWriter write = new FileWriter(f);
        BufferedWriter bf = new BufferedWriter(write);
        bf.write(convert);
        bf.close();
        wr.println("File writing done
");

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




     %>   
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
733 views
Welcome To Ask or Share your Answers For Others

1 Answer

  1. Please do not use scriptlets, write java code that you can call from your jsp.
  2. Java requires a file name with the name of the class, this was probably your main problem.
  3. What is the expected format of java code the user can enter? Method, Class or only a script inside a method?
  4. With your level of java knowledge, please do not allow unknown users to enter code or your server will get hacked very soon!

The following code compiles the code in variable convert. It automaticaly detects the name of the class entered. If no class has been found, it creates a template with the classname MyClass.

      String convert = "public class NewClass {  public static void main(String[] args) {    System.out.println("test");  }}";
  String filename = "MyClass.java";

  int i = convert.indexOf(" class ");
  if(i == -1) {
    convert = "public class MyClass { " + convert + " } ";
  } else {
    int classNameIndex = convert.indexOf(" ", i+1);
    int classNameIndexEnd = convert.indexOf(" ", classNameIndex+1);        
    filename = convert.substring(classNameIndex+1, classNameIndexEnd) + ".java";
  }

  PrintWriter wr = response.getWriter();
  File f = new File(filename);
  if (!f.exists()) {
    f.createNewFile();
    wr.println("File is created
");

  }
  FileWriter write = new FileWriter(f);
  BufferedWriter bf = new BufferedWriter(write);
  bf.write(convert);
  bf.close();
  wr.println("File writing done
");

  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  int result = compiler.run(null, null, null, filename);
  if (result == 0) {
    wr.println("Compilation Successful");
  } else {
    wr.println("Compilation Failed");
  }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...