Execute shell using the exec function of php. many commands can be executed, but there is a problem with executing java programs. [Testshell. php] is as follows: {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 use php to directly execute this file...
exec
Function execution
shell
, Many commands can be executed, but there is a problem with executing java programs.
[Testshell. php] is as follows:
The java file is also very simple. it is a test file that outputs helloworld and writes it to the file.
In addition, I usephp
Directly execute the file as follows:
$ php testshell.php
However, this php file is not reflected when it is executed in the browser.
I started to guess that it was a write Permission problem, but I wrote anotherpython
Test:
File ("output.txt"). write ("test \ n ")
Then change testshell. php:
$str="python a.py";exec($str,$output,$retnruVal);print_r($output);
This is also a success, so I don't know where the problem is-please advise.
Appendix a. java program:
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"); // relative path, if no output exists, a new output is created. Txt file writename. createNewFile (); // create a new file BufferedWriter out = new BufferedWriter (new FileWriter (writename); out. write ("I will write the file \ r \ n"); // \ r \ n is a line break out. flush (); // Press the content in the cache area into the file out. close (); // close the file} catch (Exception e) {e. printStackTrace ();}}}
Seta.java
Changed. an output is added, and the result can be output, that is, it can be executed.System.out.println
However, the subsequent code cannot be executed, and it does not feel like a Permission problem.
Public static void main (String [] args) {try {String a = "hello"; System. out. println (a); File writename = new File ("output.txt"); // relative path. if not, create a new output. Txt file writename. createNewFile (); // create a new file BufferedWriter out = new BufferedWriter (new FileWriter (writename); out. write ("I will write the file \ r \ n"); // \ r \ n is a line break out. flush (); // Press the content in the cache area into the file out. close (); // close the file} catch (Exception e) {e. printStackTrace ();}}
Reply content:
Phpexec
Function executionshell
, Many commands can be executed, but there is a problem with executing java programs.
[Testshell. php] is as follows:
The java file is also very simple. it is a test file that outputs helloworld and writes it to the file.
In addition, I usephp
Directly execute the file as follows:
$ php testshell.php
However, this php file is not reflected when it is executed in the browser.
I started to guess that it was a write Permission problem, but I wrote anotherpython
Test:
File ("output.txt"). write ("test \ n ")
Then change testshell. php:
$str="python a.py";exec($str,$output,$retnruVal);print_r($output);
This is also a success, so I don't know where the problem is-please advise.
Appendix a. java program:
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"); // relative path, if no output exists, a new output is created. Txt file writename. createNewFile (); // create a new file BufferedWriter out = new BufferedWriter (new FileWriter (writename); out. write ("I will write the file \ r \ n"); // \ r \ n is a line break out. flush (); // Press the content in the cache area into the file out. close (); // close the file} catch (Exception e) {e. printStackTrace ();}}}
Seta.java
Changed. an output is added, and the result can be output, that is, it can be executed.System.out.println
However, the subsequent code cannot be executed, and it does not feel like a Permission problem.
Public static void main (String [] args) {try {String a = "hello"; System. out. println (a); File writename = new File ("output.txt"); // relative path. if not, create a new output. Txt file writename. createNewFile (); // create a new file BufferedWriter out = new BufferedWriter (new FileWriter (writename); out. write ("I will write the file \ r \ n"); // \ r \ n is a line break out. flush (); // Press the content in the cache area into the file out. close (); // close the file} catch (Exception e) {e. printStackTrace ();}}
I don't know if there is any specific response information, but from my inference, it is irrelevant to the file path.
Is the java environment variable correct. Try to use the full java path for execution.
The permission issue has been resolved. Www-data is a default user with no permission to write files... Pitfall