15
votes

First off, if this belongs on Ask Ubuntu, my apologies...I'm not really sure what forum this falls under.

I'm attempting to fork and build an Android project from Github using PHP and a shell script. Basically, I'm printing out some output from a shell script I wrote into a website. When you visit the page, and press some buttons, the project is forked and built using Gradle. The PHP code runs the shell script, and then prints the output into the browser as the command runs.

However, I am getting this error as the output of my script:

FAILURE: Build failed with an exception.

* What went wrong:
Failed to load native library 'libnative-platform.so' for Linux amd64.

The script is simply running gradle build for now but I intend to change that later...right now, I'm just working on building the root project and outputting the result.

Here's the output if I run the command with the --stacktrace flag:

* Exception is:
net.rubygrapefruit.platform.NativeException: Failed to load native library 'libnative-platform.so' for Linux amd64.
    at net.rubygrapefruit.platform.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:49)
    at net.rubygrapefruit.platform.Native.init(Native.java:55)
    at org.gradle.internal.nativeintegration.services.NativeServices.initialize(NativeServices.java:74)
    at org.gradle.internal.nativeintegration.services.NativeServices.initialize(NativeServices.java:60)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:203)
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
    at org.gradle.launcher.Main.doAction(Main.java:33)
    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:622)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
    at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
Caused by: java.io.IOException: No such file or directory
    at java.io.UnixFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:959)
    at net.rubygrapefruit.platform.internal.NativeLibraryLocator.find(NativeLibraryLocator.java:39)
    at net.rubygrapefruit.platform.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:41)
    ... 16 more

And here is the output with the --debug flag:

06:21:43.710 [ERROR] [org.gradle.BuildExceptionReporter] 
06:21:43.740 [ERROR] [org.gradle.BuildExceptionReporter] FAILURE: Build failed with an exception.
06:21:43.747 [ERROR] [org.gradle.BuildExceptionReporter] 
06:21:43.747 [ERROR] [org.gradle.BuildExceptionReporter] * What went wrong:
06:21:43.748 [ERROR] [org.gradle.BuildExceptionReporter] Failed to load native library 'libnative-platform.so' for Linux amd64.
06:21:43.749 [ERROR] [org.gradle.BuildExceptionReporter] 
06:21:43.750 [ERROR] [org.gradle.BuildExceptionReporter] * Exception is:
06:21:43.752 [ERROR] [org.gradle.BuildExceptionReporter] net.rubygrapefruit.platform.NativeException: Failed to load native library 'libnative-platform.so' for Linux amd64.
06:21:43.753 [ERROR] [org.gradle.BuildExceptionReporter]    at net.rubygrapefruit.platform.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:49)
06:21:43.753 [ERROR] [org.gradle.BuildExceptionReporter]    at net.rubygrapefruit.platform.Native.init(Native.java:55)
06:21:43.754 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.internal.nativeintegration.services.NativeServices.initialize(NativeServices.java:74)
06:21:43.754 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.internal.nativeintegration.services.NativeServices.initialize(NativeServices.java:60)
06:21:43.755 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:203)
06:21:43.756 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
06:21:43.756 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
06:21:43.756 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
06:21:43.757 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.launcher.Main.doAction(Main.java:33)
06:21:43.758 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
06:21:43.759 [ERROR] [org.gradle.BuildExceptionReporter]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
06:21:43.759 [ERROR] [org.gradle.BuildExceptionReporter]    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
06:21:43.760 [ERROR] [org.gradle.BuildExceptionReporter]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
06:21:43.760 [ERROR] [org.gradle.BuildExceptionReporter]    at java.lang.reflect.Method.invoke(Method.java:622)
06:21:43.761 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:54)
06:21:43.761 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:35)
06:21:43.762 [ERROR] [org.gradle.BuildExceptionReporter]    at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
06:21:43.762 [ERROR] [org.gradle.BuildExceptionReporter] Caused by: java.io.IOException: No such file or directory
06:21:43.763 [ERROR] [org.gradle.BuildExceptionReporter]    at java.io.UnixFileSystem.createFileExclusively(Native Method)
06:21:43.763 [ERROR] [org.gradle.BuildExceptionReporter]    at java.io.File.createNewFile(File.java:959)
06:21:43.764 [ERROR] [org.gradle.BuildExceptionReporter]    at net.rubygrapefruit.platform.internal.NativeLibraryLocator.find(NativeLibraryLocator.java:39)
06:21:43.764 [ERROR] [org.gradle.BuildExceptionReporter]    at net.rubygrapefruit.platform.internal.NativeLibraryLoader.load(NativeLibraryLoader.java:41)
06:21:43.765 [ERROR] [org.gradle.BuildExceptionReporter]    ... 16 more
06:21:43.765 [ERROR] [org.gradle.BuildExceptionReporter]

I did some research on this matter and found this forum post, but it didn't give me any leads as to how to fix the issue.

Further research led me to a Github issue, which appears to be for NixOS, but I'm running Ubuntu 12.04 CLI. Also, I should mention I'm running Gradle 2.10 as well.

I also tried running gradle -Dorg.gradle.native=false build as well, but the outcome was the same.

Now, as far as what user is calling the script, I believe it is www-data as I'm running an Apache2 web server and PHP.

Here's a little snippet of the PHP code that runs the script, which I found on another SO post:

<?php
    $command = "sh /home/andrew/scripts/build.sh 2>&1";
    while (@ ob_end_flush()); // end all output buffers if any
    $proc = popen($command, 'r');
    echo '<pre style="border-radius: 5px; padding:4px; color:black; background:#ffffff" align="left">';
    while (!feof($proc)){
        echo fread($proc, 1024);
        @ flush();
    }
    echo '</pre>';
?>

I'm at a loss now, so I'm hoping someone can lead me to a new, reliable answer.

6
if you run this script from terminal /home/andrew/scripts/build.sh is it working as expected?Pawel Dubiel
Well, the thing is, if I fork the repo in my home directory, it works as intended. But the PHP script forks it into the web directory where the script runs /var/www/public_html/script.php, and then builds it.Andrew Quebe
I don't know anything about Gradle, but i would first check if all expected environment variables are also visible when you executing the script through PHP. for example GRADLE_USER_HOME etc.. .. And also would double check if these environment paths are writable by www-data user.Pawel Dubiel

6 Answers

9
votes

If forking and building script working fine when is executed under your user account in the terminal then a problem is most likely with environment variables and files permissions.

1 It could be that path defined in GRADLE_USER_HOME is not writable by www-data user. To change home dir you could execute gradle -g /path/writable/by-www-user just before running build script or use in PHP putenv() to amend 'GRADLE_USER_HOME'.

If you change GRADLE_USER_HOME then also move files like gradle.properties which may be important for you build.

2 Check if gradle command can be executed by www-data

sudo -u www-data `gradle --version`

How to check environment variables for www-user and for your user:

you can use in terminal printenv to print environment variables. For example for www-data user that would be sudo -u www-data printenv and for your user just printenv

you can save them to files and compare

sudo -u www-data printenv > wwwdata-env
printenv > my-env
diff --side-by-side --suppress-common-lines wwwdata-env my-env

Then use PHP method putenv() - to set any missing variables related to Gradle and the project you build.

3
votes

I have solved it by overriding GRADLE_USER_HOME enviromental variable.

In ~/.bashrc file, add the next line:

export GRADLE_USER_HOME=/home/www-data/.gradle

Where www-data is the username.

And restart your session or execute source ~/.bashrc to apply changes.

1
votes

I encounter this issue because ~/.gradle is a broken symbolic link. hope it helps.

0
votes

I am using gradle 3.0 and it needs the directory mentioned in -g parameter to have execute permission. It wasn't executable when we got this error and making it executable solved the problem.

0
votes

For me, @Pawel Dubiel answer didn't work, So I found that adding "-g gradle-user-home" to the Gradle command solved the issue.

gradle build -g gradle-user-home
0
votes

Either you are missing $GRADLE_HOME variable or the directory defined in that variable is missing. Make sure your user has write access to that directory