python - php调用shell命令: exec('java -jar a.jar') 不执行。
使用php的exec
函数来执行shell
, 很多命令都可以执行,唯独执行java程序有问题。
[testshell.php]如下:
<code><?php $str="java -jar a.jar"; exec($str,$output,$retnruVal); print_r($output); ?></code>
java文件也很简单,就是一个测试文件,输出helloworld,并写到文件。
另外我用php
直接执行这个文件,如下,成功,没有问题:
<code>$ php testshell.php</code>
但是在浏览器中执行这个php文件,没有反应。
开始猜测是写权限问题,但是又写了个python
测试:
<code>file("output.txt").write("测试\n")</code>
然后testshell.php改为:
<code>$str="python a.py"; exec($str,$output,$retnruVal); print_r($output);</code>
这样也是成功的, 所以就不知道问题处在哪里了--请指教啊。
附 a.java的程序:
<code>import java.io.File; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; public class a{ public static void main (String[] args) { try{ File writename = new File("output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件 writename.createNewFile(); // 创建新文件 BufferedWriter out = new BufferedWriter(new FileWriter(writename)); out.write("我会写入文件啦\r\n"); // \r\n即为换行 out.flush(); // 把缓存区内容压入文件 out.close(); // 最后记得关闭文件 }catch(Exception e){ e.printStackTrace(); } } }</code>
把a.java
改了,加了一个输出,结果可以输出,也就是说能够执行System.out.println
但是后续的代码无法执行, 感觉也不是权限问题啊。
<code>public static void main (String[] args) { try{ String a = "hello"; System.out.println(a); File writename = new File("output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件 writename.createNewFile(); // 创建新文件 BufferedWriter out = new BufferedWriter(new FileWriter(writename)); out.write("我会写入文件啦\r\n"); // \r\n即为换行 out.flush(); // 把缓存区内容压入文件 out.close(); // 最后记得关闭文件 }catch(Exception e){ e.printStackTrace(); } }</code>
回复内容:
使用php的exec
函数来执行shell
, 很多命令都可以执行,唯独执行java程序有问题。
[testshell.php]如下:
<code><?php $str="java -jar a.jar"; exec($str,$output,$retnruVal); print_r($output); ?></code>
java文件也很简单,就是一个测试文件,输出helloworld,并写到文件。
另外我用php
直接执行这个文件,如下,成功,没有问题:
<code>$ php testshell.php</code>
但是在浏览器中执行这个php文件,没有反应。
开始猜测是写权限问题,但是又写了个python
测试:
<code>file("output.txt").write("测试\n")</code>
然后testshell.php改为:
<code>$str="python a.py"; exec($str,$output,$retnruVal); print_r($output);</code>
这样也是成功的, 所以就不知道问题处在哪里了--请指教啊。
附 a.java的程序:
<code>import java.io.File; import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; public class a{ public static void main (String[] args) { try{ File writename = new File("output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件 writename.createNewFile(); // 创建新文件 BufferedWriter out = new BufferedWriter(new FileWriter(writename)); out.write("我会写入文件啦\r\n"); // \r\n即为换行 out.flush(); // 把缓存区内容压入文件 out.close(); // 最后记得关闭文件 }catch(Exception e){ e.printStackTrace(); } } }</code>
把a.java
改了,加了一个输出,结果可以输出,也就是说能够执行System.out.println
但是后续的代码无法执行, 感觉也不是权限问题啊。
<code>public static void main (String[] args) { try{ String a = "hello"; System.out.println(a); File writename = new File("output.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件 writename.createNewFile(); // 创建新文件 BufferedWriter out = new BufferedWriter(new FileWriter(writename)); out.write("我会写入文件啦\r\n"); // \r\n即为换行 out.flush(); // 把缓存区内容压入文件 out.close(); // 最后记得关闭文件 }catch(Exception e){ e.printStackTrace(); } }</code>
不知道有没有具体的返回信息,不过从我的推断来说,与文件路径不无关系。
java的环境变量正确么。 试试用java完整路径执行。
已经解决. 权限问题。 www-data是默认用户无写文件权限。。。坑了

Outils d'IA chauds

Undresser.AI Undress
Application basée sur l'IA pour créer des photos de nu réalistes

AI Clothes Remover
Outil d'IA en ligne pour supprimer les vêtements des photos.

Undress AI Tool
Images de déshabillage gratuites

Clothoff.io
Dissolvant de vêtements AI

AI Hentai Generator
Générez AI Hentai gratuitement.

Article chaud

Outils chauds

Bloc-notes++7.3.1
Éditeur de code facile à utiliser et gratuit

SublimeText3 version chinoise
Version chinoise, très simple à utiliser

Envoyer Studio 13.0.1
Puissant environnement de développement intégré PHP

Dreamweaver CS6
Outils de développement Web visuel

SublimeText3 version Mac
Logiciel d'édition de code au niveau de Dieu (SublimeText3)

Sujets chauds

Guide d'installation et de mise à niveau de PHP 8.4 pour Ubuntu et Debian

Comment configurer Visual Studio Code (VS Code) pour le développement PHP

Comment analysez-vous et traitez-vous HTML / XML dans PHP?

Break or Return of Java 8 Stream Forach?
