apktool command in shell script does not execute from Java program - java

I wish to execute a simple bash Script from Java. This script is as follows:
cp /home/ashish/Downloads/apktool/apktool.jar /home/ashish/workspace/MyFirstApp/bin/apktool.jar
java -jar apktool.jar d -f MyFirstApp.apk
echo "Hello World"
The problem is only the cp command is executed and the last echo is executed. The second command doesn't execute. However, if I execute the second command from the command line, it runs well (the apk folder is created).
How can I make Java program execute the apktool command from the shell script ?
Thanks.

Well, in my experience, I would say that Java is not very fond of being ran by means of script.
I did a quick google search. Try the ProcessBuilder, it looks perfect for this situation, in my opinion.
I hope this helps you! :)

Related

Perl Process can't find command svn when called from Java

I need to automatically execute some perl scripts from Java. I use Runtime.getRuntime().exec to start the perl process. I generate a start-parameter-string and pass it as an argument to exec. The String looks somewhat like this:
cmd /C start /wait perl "path\to\perl\script" -p1 scriptparameter1 -p2 scriptparameter2
I also tried
perl "path\to\perl\script" -p1 scriptparameter1 -p2 scriptparameter2.
If I copy that String and execute it via Windows+R everything works, but via exec the Perl-scripts can't find the svn-command. Why does it make a difference if I execute the perl process from java instead of directly from windows?
I figured it out. The reason was that I had just installed svn and the PATH envirment variable took effect for cmd but not for other processes (for whatevery reason). After a computer restart everything works as expected.

Writing a bash script to run a java program

I'm rank new to bash thus the question.
I've a java program that I've exported as a .jar.
This file when run as
java -jar somefile.jar
goes into an infinite loop and awaits a file name. Based on the correct file path it generates an output.
How do I write a bash script to do automated testing of this project.
I need the scrip to do the following -
Run the program, which is run the same command
provide an array of 5 files as an input to the program
For each file write the output to an log file.
This should do it.
#!/bin/bash
files="$#"
for i in $files;
do
echo "Doing $i"
java -jar somefile.jar <<< "$i"
done
Make sure you chmod u+x filename it first. Then call it like this:
./filename firstfile secondfile thirdfile etc.
Other:
As sjsam pointed out, the use of <<< is a strictly bash thing. You are apparently using bash ("I'm rank new to bash..."), but if you weren't, this would not work.
Suppose my java program is HelloWorld.java. We can run it in 2 ways:
1st using executable jar
2nd by running java class from terminal
create a new text file and name it hello.sh
In hello.sh
!/bin/bash
clear
java -jar HelloWorld.jar
Save it and open terminal:
1 navigate to directory where your HelloWorld.jar is present
2 give permission to terminal to run the bash script by using the following command
sudo chmod 754 hello.sh
3 run you script by running the following command
./hello.sh

Using maven plugin exec-maven-plugin to run a shell script

I have a java web application project. I am using the exec-maven-plugin to execute a shell script which creates a small txt file in a directory when the project is built using mvn clean isntall. I have confirmed that the script is being executed when running mvn clean install by writing test text. However, the script is not creating the txt file.
When the script is run normally through the terminal, ie ./script.sh , the txt file is created correctly.
toTxt="hello"
printf $toTxt > testText.txt
echo 'This shows up, but testText is not created.'
Does anyone know the reason why this is happpening?
Try either echoing pwd command to see where it should create the file or add <workingDirectory>${project.build.outputDirectory}</workingDirectory>
to your configuration block in pom.xml
NOTE: You can specify something other than ${project.build.outputDirectory} to specifically point to the right place. And make sure you have write permissions to it.
Keep in mind that some commands are not really external programs, but shell built-ins. This means that they don't launch programs, and maven-exec-plugin will not be able to run them. However, you can run bash to get the result, just not pwd.
bash -c "echo $(pwd)"
should do the trick. Maven is launching bash which is then running the script echo $(pwd) which calls the bash builtin function pwd and passes the result back (due to the echo).
Yes, it's a lot of work to get around the lack of a pwd program, but that's because there really isn't a pwd program, it's part of the internal offerings of bash.
http://www.tldp.org/LDP/abs/html/internal.html lists the bash internals (builtin commands).

Calling a script from Java

I have a java program that uses the JSch libraries. I can successfully call scripts from it, but when calling one script I have that calls another script, it doesn't end up calling the other one. I have tested it in command line and it works just fine. I know I am calling the script correctly from Java because part of it executes. The script is below.
MOVIE_ROOT="/path/to/Movies"
IMDB_SCRIPT="/path/to/imdb-lookup/imdb-mf.sh"
MOVIE_FOLDER="$1"
MOVIE_FILE="$2"
MOVIE_NAME=${MOVIE_FILE%.*};
# If there isn't any info for the movie
if [ ! -f "$MOVIE_ROOT/$MOVIE_FOLDER/$MOVIE_NAME.jpginfo" ] ; then
cd "$MOVIE_ROOT/$MOVIE_FOLDER"; $IMDB_SCRIPT -p -t "\"$MOVIE_NAME\"" > "$MOVIE_ROOT/$MOVIE_FOLDER/$MOVIE_NAME.mvinfo";
exit 0;
fi
cat "$MOVIE_ROOT/$MOVIE_FOLDER/$MOVIE_NAME.mvinfo";
exit 0;
I also know that imdb-mf.sh works as well. I have echod out the line cd "$MOVIE_ROOT... and pasted that into command line and it works fine. The only time it doesn't work is when I run the script from Java. All the scripts have correct permissions set and are chmod +x. Any ideas on what's going wrong?

running busybox through a .bat using java

I am trying to execute ls command through busybox.
I am creating a .bat file to execute this command which i am calling through .java
However, i am not able to execute commands one after another in .bat file.
This is the contents of my .bat file
"C:\Documents and Settings\Some Directory\Android\android-sdk\platform-tools\adb.exe" shell
/data/busybox/busybox ls
what i think that once i start the shell through the first line of my .bat, the control from the shell is lost hence second command is not executed.
Because if i write my .bat file as
"C:\Documents and Settings\Some Directory\Android\android-sdk\platform-tools\adb.exe" shell ls
it works fine.
I need to write commands in my .bat file so that they exceute one after the another.
I have tried using CALL before each commands in .bat, still it does not work.
I have tried to use multiple .bat, still a fail cause.
Can someone please help me on this?
Thanks a ton.
I am unable to test this myself with ADB at this moment, but this works for other programs that have an input buffer. I will try to verify this tonight, but if anyone else confirms it before then, leave a comment.
#echo off
( echo shell
echo /data/busybox/busybox ls
) | adb.exe

Categories

Resources