2

I wanted to make a Java tutorial website for my final year project. The website would ask the user a question and they would write the java code into a form.

What I cant figure out is how to compile the code that has been submitted. Should I write my own test cases in PHP or is there a way to get a java compiler that will run on a web host like hostmonster? Any suggestions welcome.

da1th1
  • 55
  • 10
  • http://stackoverflow.com/questions/1064259/how-can-i-compile-and-deploy-a-java-class-at-runtime – mre Sep 06 '12 at 13:12

3 Answers3

2

Have a look at the JavaCompiler class:

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 Test{" + "   public static void main (String [] args){"
        + "      System.out.println (\"Hello, World\");"
        + "      System.out.println (args.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;
  }
}

The above compiles a String containing Java source and executes it for more see: JavaCompiler Turorials - Java2s

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138
1

You can use Java Compiler to compile classes on the fly

Yes as Suggested You will require a machine where Java is installed Mostly your server.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
1

You need to be on a machine which will run Java to be able to compile it or run it. Once you have that you can use the command line.

Note: You should take some basic steps to ensure the program cannot be easily used to hack the system e.g. Runtime.exec() can be disabled.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130