Understanding command through adb shell and through code - Android

Solution 1:

The command you run with adb shell gets executed with shell(UID=2000) user privileges. The command you run from your java code gets executed with your app's UID privileges. Thus the difference.