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

WBOY
Release: 2016-08-04 09:19:01
Original
1775 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Then testshell.php is changed to:

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

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Then testshell.php is changed to:

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

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!