1

Is there a way to set up the Java compiler on a server to compile uploaded scripts? My use case is where users can upload custom java scripts/plugins for a program. Then the server would compile those scripts, and place the .jar in a folder. Then the application (the one users are developing for) would query the server for available scripts/plugins to download and use. Is this possible? If so, how?

*EDIT: I cannot install the JDK on my server, since it is a shared server. Is there any way to compile java without installing anything (a stupid question i know, but doesn't hurt to ask..)? I have php....not that that would help any.

LordZardeck
  • 7,953
  • 19
  • 62
  • 119
  • have you looked at application servers? sounds like such a setup could be used instead, that way users create apps on the app server which are then called by the 'application'. not answering your question, but maybe worth a try. – AndersK Mar 09 '11 at 04:53

3 Answers3

3

I answered a similar question here:

Dynamic code execution

In short, the Java Compiler API allows you to create compiled Java class files from Java Strings that contain source code.

Community
  • 1
  • 1
Travis Webb
  • 14,688
  • 7
  • 55
  • 109
  • I'm not wanting to do this in Java. I'm wanting to do this on the server. – LordZardeck Mar 09 '11 at 04:47
  • 1
    @LordZardeck Where does "Java run"? –  Mar 09 '11 at 04:50
  • the code needs to be compiled so it sends a ready-to-use jar file to the java application (not on the server) – LordZardeck Mar 09 '11 at 05:04
  • I would still recommend using the Java Compiler API. Otherwise you're building this system using server shell scripts, and invoking javac directly on unsanitized code uploaded by users is EXTREMELY dangerous. – Travis Webb Mar 09 '11 at 18:39
1

It's definitely possible. You basically just need the server to save the uploaded code to disk, then use Runtime#exec() to call javac on the file. You may, however, need a custom Classloader if your server is going to dynamically add new classes to its classpath.

N.B. allowing users to execute arbitrary Java on your server opens a gaping security hole.

Custom classloader recommended reading:

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
0

EDIT: Sorry if this is not what you are looking for. I assumed you are thinking about continuous integration.

Usually this is done using a source control and build system, and it's called "continuous integration". I personally am using Hg (mercurial) + Maven + Continuum.

So what happens is when I commit new scripts to mercurial, continuum will pick this up and let maven check out the changes, and build (compile) the project. Usually some unit tests or even integration tests/performance tests etc. are run as well at this time.

The resulting binary can be retrieved through the web interface (GUI) of Continuum.

Here is how it looks like.

As for downloading and using, many people "deploy" the resulting artifact (jar) to maven repositories, from which other applications can download the jar and use it. Deploying can be automated using maven as well.

Enno Shioji
  • 26,542
  • 13
  • 70
  • 109