简体   繁体   中英

Set up Java compiler on server?

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.

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.

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.

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

Custom classloader recommended reading:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM