简体   繁体   English

在服务器上设置Java编译器?

[英]Set up Java compiler on server?

Is there a way to set up the Java compiler on a server to compile uploaded scripts? 有没有办法在服务器上设置Java编译器来编译上传的脚本? My use case is where users can upload custom java scripts/plugins for a program. 我的用例是用户可以上传程序的自定义Java脚本/插件。 Then the server would compile those scripts, and place the .jar in a folder. 然后服务器将编译这些脚本,并将.jar放在一个文件夹中。 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. *编辑:我无法在我的服务器上安装JDK,因为它是一个共享服务器。 Is there any way to compile java without installing anything (a stupid question i know, but doesn't hurt to ask..)? 有没有办法编译java而不安装任何东西(我知道这是一个愚蠢的问题,但问一下是不是有害...)? I have php....not that that would help any. 我有PHP ....不是那会有所帮助。

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. 简而言之,Java Compiler API允许您从包含源代码的Java Strings创建编译的Java类文件。

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. 您基本上只需要服务器将上传的代码保存到磁盘,然后使用Runtime#exec()在文件上调用javac You may, however, need a custom Classloader if your server is going to dynamically add new classes to its classpath. 但是,如果服务器要向其类路径动态添加新类,则可能需要自定义Classloader

NB allowing users to execute arbitrary Java on your server opens a gaping security hole. NB允许用户在您的服务器上执行任意Java会打开一个巨大的安全漏洞。

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. 我个人正在使用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. 那么当我将新脚本提交给mercurial时,会发生什么事情,continuum将选择它并让maven检查更改,并构建(编译)项目。 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. 可以通过Continuum的Web界面(GUI)检索生成的二进制文件。

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. 至于下载和使用,许多人将生成的工件(jar)“部署”到maven存储库,其他应用程序可以从中下载jar并使用它。 Deploying can be automated using maven as well. 部署也可以使用maven自动化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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