Home  >  Article  >  Backend Development  >  python - php calls shell command: exec('java -jar a.jar') does not execute.

python - php calls shell command: exec('java -jar a.jar') does not execute.

WBOY
WBOYOriginal
2016-08-04 09:19:011775browse

Use PHP's exec function to execute shell. Many commands can be executed, but there is a problem with executing java programs.
[testshell.php] is as follows:

<code><?php
$str="java -jar a.jar";
exec($str,$output,$retnruVal);
print_r($output);
?></code>

The java file is also very simple. It is a test file that outputs helloworld and writes it to the file.
In addition, I used php to directly execute this file, as follows, and it was successful without any problems:

<code>$ php testshell.php</code>

But when I execute this php file in the browser, there is no response.
At first I guessed it was a write permission issue, but I wrote another pythontest:

<code>file("output.txt").write("测试\n")</code>

Then testshell.php is changed to:

<code>$str="python a.py";
exec($str,$output,$retnruVal);
print_r($output);</code>

This is also successful, so I don’t know where the problem lies - please give me some advice.
Attached is the program 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>

I changed a.java and added an output. The result can be output, which means that System.out.println can be executed, but the subsequent code cannot be executed. It doesn’t seem to be a permissions issue.

<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>

Reply content:

Use PHP's exec function to execute shell. Many commands can be executed, but there is a problem with executing java programs.
[testshell.php] is as follows:

<code><?php
$str="java -jar a.jar";
exec($str,$output,$retnruVal);
print_r($output);
?></code>

The java file is also very simple. It is a test file that outputs helloworld and writes it to the file.
In addition, I used php to directly execute this file, as follows, and it was successful without any problems:

<code>$ php testshell.php</code>

But when I execute this php file in the browser, there is no response.
At first I guessed it was a write permission issue, but I wrote another pythontest:

<code>file("output.txt").write("测试\n")</code>

Then testshell.php is changed to:

<code>$str="python a.py";
exec($str,$output,$retnruVal);
print_r($output);</code>

This is also successful, so I don’t know where the problem lies - please give me some advice.
Attached is the program 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>

I changed a.java and added an output. The result can be output, which means that System.out.println can be executed, but the subsequent code cannot be executed. It doesn’t seem to be a permissions issue.

<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>

I don’t know if there is any specific return information, but from my inference, it has something to do with the file path.

Are the Java environment variables correct? Try executing with java full path.

Already solved. Permission issue. www-data is the default user without write file permissions. . . Tricked

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn