python-php call shell command: exec (' Java-jar A.jar ') does not execute.

Source: Internet
Author: User
Tags call shell
Using PHP's execfunction to perform shell, many commands can be executed, but there is a problem with executing Java programs.
[testshell.php] as follows:


  
   

Java file is also very simple, is a test file, output HelloWorld, and write to the file.
In addition I use php direct execution of this file, such as Next, success, no problem:

$ php testshell.php

But in the browser to execute this PHP file, no response.
Start guessing is a write permission problem, but it also writes a python test:

file("output.txt").write("测试\n")

Then testshell.php instead:

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

This is also successful, so you do not know where the problem is-please advise AH.
Procedures attached to A.java:

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();  }}}

a.javachange, add an output, the result can be output, that is, can be executed System.out.println but subsequent code can not be executed, the feeling is not a permission problem ah.

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();  }}

Reply content:

Using PHP exec functions to execute shell , a lot of commands can be executed, but the implementation of Java program has a problem.
[testshell.php] as follows:


  
   

Java file is also very simple, is a test file, output HelloWorld, and write to the file.
In addition I use php direct execution of this file, such as Next, success, no problem:

$ php testshell.php

But in the browser to execute this PHP file, no response.
Start guessing is a write permission problem, but it also writes a python test:

file("output.txt").write("测试\n")

Then testshell.php instead:

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

This is also successful, so you do not know where the problem is-please advise AH.
Procedures attached to A.java:

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();  }}}

a.javachange, add an output, the result can be output, that is, can be executed System.out.println but subsequent code can not be executed, the feeling is not a permission problem ah.

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();  }}

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

is the environment variable in Java correct? Try the Java full path execution.

has been resolved. Permissions issues. Www-data is the default user no write file permission ... The pit.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.