Get the Main Package Name of an .apk File - java

I have to launch an application on android emulator using command line.for that I have knew that there is a method by adb command;
adb shell am start -a android.intent.action.MAIN -n com.example.myApplication
but I have to find the application package name before executing this command, so anybody help me to find the package name, or any alternate solution for running apk on emulator

On Linux, simply use this command to get package name:
aapt dump badging {apk-file.apk} | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g
Getting main (launcher) activity is similar:
aapt dump badging {apk-file.apk} | grep launchable-activity: | awk '{print $2}' | sed s/name=//g | sed s/\'//g
For my personal usage, I've written a shell script called apkinfo.sh as following:
#!/bin/bash
package=`aapt dump badging $* | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g`
activity=`aapt dump badging $* | grep launchable-activity: | awk '{print $2}' | sed s/name=//g | sed s/\'//g`
echo
echo package : $package
echo activity: $activity
And use it as following:
./apkinfo.sh {path-to-apk-file.apk}

aapt dump badging <path-to-apk>

extract the apk file and rebuilt the manifest file, then by parsing the xml manifest file you can find the main activity package name.
refer this link for parsing, and refer this link for extracting apk file.

This is what I use for this in one of my projects, inside a shell script.
apk= #apk path
package=`.//aapt dump badging $apk | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g`
echo $package
adb install $apk
if you want to just enter in cmd prompt, then it would be
.//aapt dump badging ***apk relative path*** | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g
that should work.

You can use Andgroguard
This is how I did in Python3:
from androguard.misc import AnalyzeAPK
a, d, dx = AnalyzeAPK(apk_file)
package_name = a.get_package()
print(package_name)

Related

Execute bash commands via Java in Ubuntu

I have such command:
top -bn1 | grep \"Cpu(s)\" | sed \"s/.*, *\\([0-9.]*\\)%* id.*/\\1/\" | awk '{print 100 - $1}
I tried to run it with ProcessBuilder or Runtime, but I get nothing. Why?

Stop Java Process if Running in Raspbian

I want to make sure that a java process in Raspbian is stopped before launching a new instance of it.
My approach, until now, has been to try to create a one-line command to stop the process from the command line, as it is the easiest way to include this step in the GO continuos delivery tool. This far, I was able to come up with this solution, which effectively stops the process:
kill $(jps -m | grep NameOfTheMainClass | awk '{print $1}')
But the problem is that when the process was not started, the kill fails as it has no pid to signal. How could I fix that?
If you prefer a one-liner
JPID=$(jps -m | grep NameOfTheMainClass | awk '{print $1}') && [ -z "$JPID" ] || kill $JPID
Try to make it conditional:
export MAIN_CLASSNAME=""
isRunning()
{
PID=`ps ax | grep MAIN_CLASSNAME | grep -v grep | awk '{print $1}'`
is_running="false"
if [ ! -z "$PID" ]; then
is_running="true"
fi
test "$is_running" = "true"
}
if isRunning; then
kill $PID
fi

java How can I find out the original username a process was started with?

How would I get to know the real/ogiginal username ?
On linux, we can run the below command, which would give the real user name based on the link but what is the java equivalent of the same ?
REALUSERNAME=$(ps uhp `ps -AjH | grep \`ps -u $USER fh | awk '{ print $0; if(index($0, "ps -u $USER fh")) exit 0;}' | tac | awk '{if(!index($0, "\\\\\_")){print $1; exit 0;}}'\` | awk '{print $3}'` | awk '{print $1}')
How can I find out the original username a process was started with?

Jboss 7.1.1 start/stop script

Could someone tell how to start/stop the Jboss-7.1.1 server in MAC using Shell Script.
stop_viewer(){
echo "********* Stopping JBoss Server by killing the process **********";
ps | grep domain.sh | grep -v grep | awk '{print $1}' | xargs kill
ps | grep java | grep -v grep | awk '{print $1}' | xargs kill
ps -ef | grep superuser | grep java | grep -v grep | awk '{print $2}'| xargs kill
echo "********* Stopped JBoss Server by killing the process **********";
}
The above script is working fine in Jboss-7.0.2 to stop the server. But in Jboss-7.1.1, it doesn't stop the server. Please someone help to solve this.
1) First you need to have JBoss downloaded. (I assume you already have valid Java version installed).
2) Once it is downloaded, unzip the folder:
cd /Users/eugene/Downloads
mkdir JBOSS-7
cp /Users/eugene/Downloads/jboss-as-7.1.1.Final.zip /Users/eugene/Downloads/JBOSS-7
cd /Users/eugene/Downloads/JBOSS-7
unzip /Users/eugene/Downloads/jboss-as-7.1.1.Final.zip
3)
cd Users/eugene/Downloads/JBOSS-7/jboss-as-7.1.1.Final/bin
./standalone.sh
If you want to stop it:
ctrl + c
of course your path may be different. If you want to run it in background, then just do:
./standalone.sh &
Stopping it :
ps -ef | grep jboss
You will get an output close to this one:
eugene#eugenes-MacBook-Pro ~/D/J/j/bin> ps -ef | grep jboss
501 1471 1446 0 1:32AM ttys000 0:03.31 /usr/....
And then issue:
kill -9 1471
Finally with JBoss CLI you can execute:
./jboss-cli.sh --connect ":shutdown"
EDIT
The Script seems to do it's job, all you have to do is edit it a bit:
#!/bin/sh
echo "********* Stopping JBoss Server by killing the process **********";
ps -e | grep jboss | grep -v grep | awk '{print $1}' | xargs kill
echo "********* Stopped JBoss Server by killing the process **********";
Notice that I removed a few lines and changed java with jboss
Put this in a file called stopJboss.sh
Then :
sudo chmod +x stopJBoss.sh
Then invoke it when needed:
./stopJBoss.sh
This will work only if you have a single instance of JBoss running, for more you will need a different script.
P.S. I am not a guru in scripting but here is what this line does:
ps -e | grep jboss | grep -v grep | awk '{print $1}' | xargs kill
It is going to look for every process that contains the jboss keyword. But it also going to output the grep command itself, thus you will get an output of two commands, but you need only the first one.
You could run ps -e | grep jboss and see that the output contains two lines and not one.
That is why you invoke grep -v grep - which means : in those two lines found grep for "grep" but invert the result, in this way you omit the second unneeded result.
Then awk '{print $1}' splits the string into tokens and takes the first one, which is the PID that you need and then you pass this PID to the kill command using the xargs command.
To shutdown the server via command line
sh ./bin/jboss-cli.sh --connect command=:shutdown
assuming you are running on localhost and using the default native management port i.e. 9999
if not you need to specify the IP (jboss.bind.address) and the native management port(jboss.management.native.port) configured in standalone.xml
sh ./bin/jboss-cli.sh --connect controller=<IP>:<native-mgmt-port> command=:shutdown
This is how I do it:
ps -ef | grep jboss | grep -v grep | awk '{print $2}' | xargs kill -9
I have this in a bash file that i call killjboss and it works well with me.
After dive on the Google, i managed to put this work:
#!/bin/sh
### BEGIN INIT INFO
# Provides: jboss
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop JBoss AS v7.1.1
### END INIT INFO
#
#source some script files in order to set and export environmental variables
#as well as add the appropriate executables to $PATH
export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=$JAVA_HOME/bin:$PATH
export JBOSS_HOME=/home/gaspar/jboss-as-7.1.1.Final
export PATH=$JBOSS_HOME/bin:$PATH
case "$1" in
start)
echo "Starting JBoss AS 7.1.1"
#original:
#sudo -u jboss sh ${JBOSS_HOME}/bin/standalone.sh
#updated:
start-stop-daemon --start --quiet --background --chuid jboss --exec ${JBOSS_HOME}/bin/standalone.sh
;;
stop)
echo "Stopping JBoss AS 7.1.1"
#original:
#sudo -u jboss sh ${JBOSS_HOME}/bin/jboss-admin.sh --connect command=:shutdown
#updated:
sudo -u jboss sh ${JBOSS_HOME}/bin/jboss-cli.sh --connect command=:shutdown
;;
*)
echo "Usage: /etc/init.d/jboss {start|stop}"
exit 1
;;
esac
exit 0
:)

Shell script to stop a java program

Is there a way to stop a java program running using a shell script by knowing the name alone.I am using ksh shell
following up on Mnementh' suggestion:
this should do the job
jps -l | grep org.example.MyMain | cut -d ' ' -f 1 | xargs -rn1 kill
jps -l: list java process with "full package name for the application's main class or the full path name to the application's JAR file."
grep: choose the process you like
cut -d -' ' -f 1: split the output in columns using delimiter ' ' and print only the first one (the pid)
xargs -rn1 kill: execute kill for each PID (if any)
note that you must run jps and xargs with the same user (or root) as you're running the process
Add a unique property to the JVM to identify it easily, e.g. for test.class
java -Duniquename=1 test
To kill it:
ps ax | grep uniquename | grep -v grep | awk '{print $1}' | xargs kill
You can use jps identifying the process-id associated with the name of the started java-program (jps is a process-manager for java-programs). With this id you can kill the process normally.
You can use pkill:
pkill your_java_program_name
This would work if you run only one instance of the your program is running.
you can use -o option of ps to format your output,
ps -eo cmd,pid | awk '!/awk/&&/mycommand/{cmd="kill -9 "$2;system(cmd)}'

Categories

Resources