如何创建一个 Web 应用程序来在线编译和运行 Java/C/PHP 代码?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25967639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Create a Web App to Compile and Run Java/C/PHP Code Online?
提问by sushil bharwani
Though this is a question with broader scope, I want to write a Online Test Code for my company where people can be given questions to write code in java/php/c etc and the code run and compiles online. I have seen this happening in site like codeacademy, Udacity etc. Just want to understand the architecture behind it. I have searched a lot along the similiar lines a lot on Google but couldnt find a concrete answer. Though after reading bits and pieces here and there i understood that the code is sent to compiler on server and then the results are sent back. Not sure how exactly that happens. Can somebody point me to a starting point of it.
虽然这是一个范围更广的问题,但我想为我的公司编写一个在线测试代码,人们可以在其中提出问题以在 java/php/c 等中编写代码,并且代码可以在线运行和编译。我在 codeacademy、Udacity 等网站上看到过这种情况。只是想了解它背后的架构。我在谷歌上搜索了很多类似的东西,但找不到具体的答案。虽然在这里和那里阅读了一些零碎的东西后,我明白代码被发送到服务器上的编译器,然后结果被发回。不知道这到底是怎么发生的。有人可以指出我的起点吗?
采纳答案by Manu
What you can basically have, according to a MVC pattern applied to a web architecture, is something like this:
根据应用于 Web 架构的 MVC 模式,您基本上可以拥有的是这样的:
- A web application client-side, which allows the user to insert some code, possibly leveraging Javascript for early syntactic check
- A server endpoint, receiving the inserted code as input from the client
- Web 应用程序客户端,允许用户插入一些代码,可能利用 Javascript 进行早期语法检查
- 服务器端点,从客户端接收插入的代码作为输入
The sequence of operations could be:
操作顺序可以是:
- Server-side, the input is transformed into the appropriate structure for the target programming language, e.g. a Java class or a C module.
- Possibly, more context is defined (e.g. a classpath).
- Then, if the language is compiled, the compiler is invoked (e.g. javac or gcc). This can happen in several ways, e.g. exec in C or Runtime.getRuntime().exec in Java. Otherwise the code can be deployed on a server or some simulators can be run and passed the code.
- Subsequently, the code is executed and output is intercepted (e.g. by directioning the console output to a file or just leveraging the target language infrastructure, like in this example). The execution can happen through command line (e.g. java) or via other tools (e.g. curl for running a deployed php code as it was a client browser accessing it)
- Last step for the server is to send back the intercepted output to the client in a readable format, e.g. HTML. As an alternative, if you used Java, you could go for Applet, which doesn't change the basic architecture.
- 在服务器端,输入被转换为目标编程语言的适当结构,例如 Java 类或 C 模块。
- 可能定义了更多上下文(例如类路径)。
- 然后,如果语言被编译,则调用编译器(例如 javac 或 gcc)。这可以通过多种方式发生,例如 C 中的 exec 或 Java 中的 Runtime.getRuntime().exec。否则,代码可以部署在服务器上,或者可以运行一些模拟器并传递代码。
- 随后,执行代码并截取输出(例如,通过将控制台输出定向到文件或仅利用目标语言基础结构,如本例中所示)。执行可以通过命令行(例如 java)或通过其他工具(例如 curl 用于运行已部署的 php 代码,因为它是访问它的客户端浏览器)
- 服务器的最后一步是以可读格式(例如 HTML)将截获的输出发送回客户端。作为替代方案,如果您使用 Java,您可以选择Applet,它不会改变基本架构。
However, more in general, the point is that compilers and interpreters are base software. They are not intended for general users, which can easily live with the Operating System only. Therefore, "on line compiling", at the best of my knowledge, is something different from "posting code, letting it execute on a server, and visualizing the answer". Online compiling would mean distributing the responsibility of compiling across the network, which does make sense, but, in my opinion, it is not meant to use for demonstrative purpose (like you are mentioning).
然而,更一般地说,重点是编译器和解释器是基础软件。它们不适用于一般用户,他们只能轻松地使用操作系统。因此,据我所知,“在线编译”与“发布代码,让它在服务器上执行,并可视化答案”不同。在线编译意味着在网络上分配编译的责任,这确实有道理,但是,在我看来,它并不意味着用于演示目的(就像您提到的那样)。