我正在构建一个 php 应用程序。在脚本中,有一个执行 jar 文件的调用:
?php
exec('java -jar simulations/simulation.jar');
?>
问题是这个命令行执行的是jar文件:
user@ubuntu: php execSimulation.php
但不是来自网页的调用。这个电话是由 AJAX 发出的,我是不是错过了什么??
<!-- Script to execute the simulation -->
<script type="text/javascript" src="prototype.js"></script>
<script>
function sendRequest() {
new Ajax.Request("ejecutarSimulacion.php",
{
method: 'post',
postBody: 'name='+ $F('name'),
onComplete: showResponse
});
}
function showResponse(req){
alert(req.responseText);
}
</script>
<form id="test" onsubmit="return false;">
<input type="hidden" value="<?php echo $simulacion; ?>" name="name" id="name" >
<input type="submit" value="<?php echo $nombre; ?>" onClick="sendRequest()">
</form>
例如,当我尝试只打印我发送的参数时,警报会显示它,所以我确定调用正在到达服务器,但我不知道为什么 jar 文件没有被执行。有什么想法吗?
提前致谢。
编辑
错误轨迹:
No protocol specified
Exception in thread "main" java.lang.InternalError: Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
at sun.awt.X11GraphicsEnvironment.initDisplay(Native Method)
at sun.awt.X11GraphicsEnvironment.access$200(X11GraphicsEnvironment.java:62)
at sun.awt.X11GraphicsEnvironment$1.run(X11GraphicsEnvironment.java:178)
at java.security.AccessController.doPrivileged(Native Method)
at sun.awt.X11GraphicsEnvironment.<clinit>(X11GraphicsEnvironment.java:142)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment(GraphicsEnvironment.java:82)
at java.awt.Window.init(Window.java:385)
at java.awt.Window.<init>(Window.java:438)
at java.awt.Frame.<init>(Frame.java:419)
at java.awt.Frame.<init>(Frame.java:384)
at javax.swing.JFrame.<init>(JFrame.java:174)
at org.opensourcephysics.tools.TranslatorTool.<init>(Unknown Source)
at org.opensourcephysics.tools.TranslatorTool.<clinit>(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at org.opensourcephysics.display.OSPRuntime.<clinit>(Unknown Source)
at org.opensourcephysics.controls.OSPLog.fine(Unknown Source)
at org.opensourcephysics.tools.ResourceLoader.addSearchPath(Unknown Source)
at _users.tanqueCalentamiento.TanqueCalentamiento.<clinit>(TanqueCalentamiento.java:18)
Could not find the main class: _users.simulation.Simulation. Program will exit.
最佳答案
看来您的 jar 需要 X 服务器才能运行。
Can't connect to X11 window server using ':0.0' as the value of the DISPLAY variable.
当您从命令行运行它时,您是否正在运行 X?如果这样做,这将解释为什么它在那里工作,而不是在 PHP 中工作。
您可以尝试从 PHP“劫持”正在运行的 X session 。
exec('DISPLAY=:0 java -jar simulations/simulation.jar');
您可能必须首先从命令行运行 xhost +localhost
(或 xhost +
),以允许正在运行的用户 PHP 连接到 X。
关于php - 执行php脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7097553/