1

I wan't to create an applet that includes the Java compiler and is able to download and compile .java files from the web (or from a textfield for that matter). From what I understand, the Java compiler is based on Java, so this should be possible. However, one thing I wonder is wether the Java compiler is able to store the class file in memory or that it needs explicit disk access. Also, I want to catch the errors javac produces, but this is natural.

Is there an article on this somewhere maybe? I couldn't find much on the case. What troubles could I walk into?

EDIT: I'm planning on using the applet with OpenCL, so I would have to sign the applet.

EDIT: Similar question here: Hosting the Java compiler in an applet or in Google AppEngine?

Community
  • 1
  • 1
RobotRock
  • 4,211
  • 6
  • 46
  • 86

2 Answers2

1

Java compiler is always with you. You should use java.lang.Compiler class. But I believe you will have issues with SecurityManager.

If I were you I'd prefer to compile code on server side. It means that I'd develop servlet that is able to get the java source and compile it. The resulted java class should be available over HTTP under path that is configured to be accessible by applet, so your applet will be able to run this class after compilation.

AlexR
  • 114,158
  • 16
  • 130
  • 208
1

From the documentation on the SSCCE Text Based Compiler:

  • javax.tools.ToolProvider. The docs for getSystemJavaCompiler() mention that it returns "the compiler provided with this platform or null if no compiler is provided", but do not make clear why it might be null, nor the fact that the compiler will not be available to applets or webstart apps.
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • There's a webstart link just above the text you cited. I figure it'll work if you provide a tools.jar. – RobotRock Aug 04 '11 at 13:26
  • 1
    Yes. If the user has a JDK installed, just do as I did in that code, and get them to identify where it is. See also the notes under [Add the compiler to the application's runtime classpath](http://pscode.org/stbc/help.html#classpath). – Andrew Thompson Aug 04 '11 at 14:00
  • If I would supply my webstart with OpenJDK's tools.jar, would one be able to compile with only a JRE then? – RobotRock Aug 04 '11 at 14:52
  • What happened when you tried it? I have not, but would be interested in your report. – Andrew Thompson Aug 04 '11 at 16:11
  • At the moment I have no dedicated computer to try it on, I will so in a couple of days. Problem is I couldnt find tools.jar online, so have to install the complete JDK first. – RobotRock Aug 04 '11 at 17:03
  • It worked with the Sun tools.jar as well as the OpenJDK tools.jar, so you might want to incorporate that in your STBC (is that correct?) tool ;) Maybe you could help me out with the Webstart issue that comes impaired with it: http://stackoverflow.com/questions/7093935/java-webstart-attempt-to-sandboxed-jar – RobotRock Aug 17 '11 at 14:02