16

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.

Community
  • 1
  • 1
Andrew Quebe
  • 2,263
  • 5
  • 25
  • 53
  • if you run this script from terminal /home/andrew/scripts/build.sh is it working as expected? – Pawel Dubiel Jul 24 '16 at 20:37
  • 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 Jul 24 '16 at 20:48
  • 1
    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 Jul 24 '16 at 22:20

7 Answers7

11

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.

Pawel Dubiel
  • 18,665
  • 3
  • 40
  • 58
  • I'll experiment with this...thanks for the suggestions as this didn't occur to me. – Andrew Quebe Jul 25 '16 at 01:52
  • @AndrewQuebe here is a similar issue and is also down for running with wrong user https://discuss.gradle.org/t/started-getting-libnative-platform-so-error-running-as-non-root-user/15593 ... – Pawel Dubiel Jul 25 '16 at 01:57
  • Calling `gradle -g` results in the same error...even if I call that as a logged in user over SSH. Calling `gradle --v` as www-data causes the exception as well, while calling it as my main user does not. It's installed natively...I didn't want to use the local wrappers in the project. `/etc/environment` shows no signs of any GRADLE_USER_HOME variable...should I define one? – Andrew Quebe Jul 25 '16 at 02:02
  • I think don't look into /etc/environment just use printenv in terminal if you have GRADLE_USER_HOME variable for your main user then you should also define it for www-data (with a new path writable by www-data ) I will try this week to install gradle and replicate your problem with php. – Pawel Dubiel Jul 25 '16 at 03:28
  • Another idea is to run php-resque lib add build job to queue and execute everything from cron under your user – Pawel Dubiel Jul 25 '16 at 03:29
  • 1
    @AndrewQuebe I tried a similar environment but can't replicate your problem. Build through PHP working just fine ( Ubuntu 14.04.4 LTS ) but I am running older gradle 1.4. Also my build.gradle is very simple and just printing "hello world". Maybe try to add into build.sh file `cd` into the correct directory with project. – Pawel Dubiel Jul 30 '16 at 20:49
  • I'm on 12.04 so maybe that's why. – Andrew Quebe Jul 30 '16 at 21:00
  • @AndrewQuebe I don't know. To avoid such problems I usually run everything on the server with Docker and docker-compose (it's more work to setup but great time saver later). So I run always pretty much the same configuration in production and development without any nasty surprises in production ( maybe try to spin with VirtualBox a new Ubuntu machine ). Maybe it's down to this that my build in build.gradle is very simple. – Pawel Dubiel Jul 30 '16 at 21:40
  • I'm basically trying to build Android apps through PHP code which will be done for a userbase. I need something that I can monitor the builds for error checking, but Gradle is the only thing that compiles Android apps. – Andrew Quebe Jul 30 '16 at 23:29
  • Does `gradlew` allow you to bypass specifying `GRADLE_HOME`? Mine is running on a server and I'm hoping I don't have to modify my `Jenkinsfile`. – Sridhar Sarnobat Aug 10 '22 at 22:27
6

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.

Estuardolh
  • 111
  • 1
  • 6
1

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

wukong
  • 2,430
  • 2
  • 26
  • 33
0

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.

user872858
  • 770
  • 7
  • 15
0

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
Shahar Hamuzim Rajuan
  • 5,610
  • 9
  • 53
  • 91
0

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

0

Solution

In my case this was happening on Jenkins because I didn't regenerate AND source control my gradle-wrapper.jar file.

It's not enough to just source-control gradle.properties

My error

Newer versions of Gradle may have a more obvious error message than this.

net.rubygrapefruit.platform.NativeException
...
Caused by: java.lang.UnsatisfiedLinkError: /root/.gradle/native/21/linux-amd64/libnative-platform.so: Error loading shared library libstdc++.so.6: No such file or directory

Credits

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106