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.

1
  • 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. Commented Mar 9, 2011 at 4:53

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not wanting to do this in Java. I'm wanting to do this on the server.
@LordZardeck Where does "Java run"?
the code needs to be compiled so it sends a ready-to-use jar file to the java application (not on the server)
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.
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:

4 Comments

How would I set this up on linux?
@LordZardeck: well, what server are you using? Is your application running on a servlet container (like Tomcat) or application server (like JBoss, GlassFish, WebSphere etc.)? What is this "application?"
It's an apache server I believe.
Well, it's not compiled IMMEDIATELY. It's first evaluated by moderators.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.