3

I want to execute following command on my Ubuntu server using php:

android create project --target 8 --name $fname --path ./$fname --activity MainActivity --package $fpack 2>&1

The above command is to create an Android app project. So when I enter this command on my terminal then it works fine, but when I execute it via PHP:

<?php
$fname = $_POST['fname'];
$fpack = $_POST['fpack'];
$email = $_POST['email'];

// Creating a new Android project.
var_dump(shell_exec("android create project --target 8 --name $fname --path ./$fname --activity MainActivity --package $fpack 2>&1"));

?>

When I run my PHP script I get the following output:

string(26) "sh: 1: android: not found "

Why it works when I enter manually in terminal (from user 'ashish' account) but not with php? my apache user and group is same (ashish). Any help will be appreciated :)

3
  • What if you give the full path to android executable?
    – Dan
    Feb 25, 2015 at 8:13
  • @Dan it works with full path but i need to type too much for that. any way to make it work with just 'android''? just typing android works in terminal
    – Ashish
    Feb 25, 2015 at 8:19
  • i thibnk I got a way, let me try using php putenv to set environment variables and report back
    – Ashish
    Feb 25, 2015 at 8:25

2 Answers 2

1

The question is a bit old, but for those who experience this problem can try to set the environment variables of direct on the server. PHP uses the putenv () function.

Example:// Set Variable Enviromental

$JAVA_HOME = "/usr/lib/jvm/java-8-oracle"; 
$ANDROID_HOME = "/opt/android-sdk-linux";
$PATH="$JAVA_HOME/bin:/usr/local/bin:/usr/bin:/bin:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools:$ANDROID_HOME/build-tools/24.0.0-preview";

putenv ("JAVA_HOME = $JAVA_HOME"); 
putenv ("PATH = $PATH");
0

Most likely, your $PATH is set incorrectly. Specify the full path to android, or set your path via something like this:

<?php
putenv(
    implode(PATH_SEPARATOR, array('/dir/containing/android', getenv('PATH')))
);

$fname = $_POST['fname'];
$fpack = $_POST['fpack'];
$email = $_POST['email'];

// Create a new Android project
var_dump(shell_exec("android create project --target 8 --name $fname --path ./$fname --activity MainActivity --package $fpack 2>&1"));

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .