


python - php calls shell command: exec('java -jar a.jar') does not execute.
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 python
test:
<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 python
test:
<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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

Python excels in automation, scripting, and task management. 1) Automation: File backup is realized through standard libraries such as os and shutil. 2) Script writing: Use the psutil library to monitor system resources. 3) Task management: Use the schedule library to schedule tasks. Python's ease of use and rich library support makes it the preferred tool in these areas.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
