在服务器上执行java代码 [英] Execute java code on a server
问题描述
我正在开发一个Web应用程序,一种用于编写和编译代码的在线IDE。编程语言是在大学内部和编译器内部开发的。
I'm developping a Web app, a sort of an online IDE to write and compile code. The programming language is developped internally at the university and also the compiler.
我的问题是:是否可以在服务器上执行编译器(编译器是用java编写的) ),以便它编译代码并返回要下载的编译文件?
My question is : is it possible to execute a compiler on a server ( the compiler is written in java ), so that it compiles the code and returns a compiled file to be downloaded ?
以更简单的方式,用户使用在线代码编辑器,然后点击编译按钮,服务器获取编写的代码,执行服务器上的编译器(编译器用java编写)然后返回编译后的文件。
In a simpler fashion, the user uses the online code editor, then clicks on the compile button, the server takes the written code, executes the compiler which is on ther server ( the compiler is written in java ) and then returns the compiled file.
那怎么可能我在服务器上执行编译器(用java编写)?
So how could i execute the compiler ( written in java ) on the server ?
提前谢谢!
推荐答案
您没有说出什么类型的服务器,或者您使用什么语言来开发Web应用程序(PHP,node.js,python,perl等),但通常Java发行版都有命令行二进制文件,它将运行Java代码。
You didn't say what type of server, or what language you are using to develop the web app (PHP, node.js, python, perl, etc.), but normally Java distributions have a commandline binary that will run Java code.
如果编译器文件在jar文件中,那么webapp执行的命令可能很简单:
If the compiler file is in a jar file, your command that the webapp executes could be a simple as something like:
java -jar compiler.jar inputcodefile outputexecutablefile
当然你会替换文件名并添加正确的文件名编译器需要的选项(如果有的话)。
Of course you would substitute filenames and add the proper options needed for the compiler (if any).
编辑:我看到你用node.js标记了你的问题,所以我假设这是你在服务器上使用的语言侧。
I see you tagged your question with node.js, so I'm assuming that is the language you are using on the server side.
node.js具有子进程,允许您执行外部命令。
因此,通过上面给出的示例命令,您可以执行以下操作:
node.js has "child process" that allows you to execute external commands. So with the example command I gave above, you would do something like:
var exec = require('child_process').exec;
var compileit = 'java -jar compiler.jar inputcodefile outputexecutablefile';
exec(compileit, function(error, stdout, stderr) {
});
使用PHP它更容易:
exec('java -jar compiler.jar inputcodefile outputexecutablefile');
参见 http://php.net/manual/en/function.exec.php ,了解有关PHP中exec()函数的更多信息。
See http://php.net/manual/en/function.exec.php for more info on the exec() function in PHP.
这篇关于在服务器上执行java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!