Table of Contents
Reply content:
Home Backend Development PHP Tutorial 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.

Aug 04, 2016 am 09:19 AM
java php python

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

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP's Purpose: Building Dynamic Websites PHP's Purpose: Building Dynamic Websites Apr 15, 2025 am 12:18 AM

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 vs. JavaScript: Community, Libraries, and Resources Python vs. JavaScript: Community, Libraries, and Resources Apr 15, 2025 am 12:16 AM

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: An Introduction to the Server-Side Scripting Language PHP: An Introduction to the Server-Side Scripting Language Apr 16, 2025 am 12:18 AM

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 and the Web: Exploring its Long-Term Impact PHP and the Web: Exploring its Long-Term Impact Apr 16, 2025 am 12:17 AM

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.

Why Use PHP? Advantages and Benefits Explained Why Use PHP? Advantages and Benefits Explained Apr 16, 2025 am 12:16 AM

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.

How to run programs in terminal vscode How to run programs in terminal vscode Apr 15, 2025 pm 06:42 PM

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: Automation, Scripting, and Task Management Python: Automation, Scripting, and Task Management Apr 16, 2025 am 12:14 AM

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.

Is the vscode extension malicious? Is the vscode extension malicious? Apr 15, 2025 pm 07:57 PM

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.

See all articles