running busybox through a .bat using java - 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

Related

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

How to run batch file with parameters in java?

I have created a jar file called test.jar under C:\jars. I have JAR file under the same location named run.bat and it contains the below code -
#echo off
set exec_path=C:\jars java -cp %exec_path%/test.jar; com.mycomp.myapp.MyProgram "%1"%*
#echo on
It is running successfully from command prompt with parameters.
Now I would like to run it from another JAVA program.
Please suggest.
Thanks!
I've encountered this issue before. The answer is that you have to run cmd.exe or bash or whatever shell you've got, then feed in the command to that process via the process input/output streams.
Process p = Runtime.getRuntime().exec("cmd");
p.getOutputStream().write("mybatch.bat\n");

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).

apktool command in shell script does not execute from Java program

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! :)

How do I open a .bat containing a GUI in a java program in Linux?

everyone. I'm quite new here so please be tolerant if I make any mistakes.
I have a .bat file containing a command line to open up a .jar file that contains a program that has a GUI in it. The only line that's in the .bat file is:
java -jar "NewServer.jar"
I've been trying to use Runtime() to get this to run, but most the instructions I find to open a .bat file in a java program are for Windows. I'm currently using Fedora 12 (don't tell me to upgrade, I can't) if that makes a difference and programming using Eclipse. I also found this ProcessBuilder thing, but I couldn't get it to work so unless you have very explicit directions on how to use it, please don't include it in your answer. I would much rather use Runtime. It looked simpler.
Here's my code to test using Runtime in a java program. I'm hoping that if I can get this to work, I can get it to work in my real program.
import java.io.IOException;
public class testbat {
public static void main(String[] args) {
Process proc = null;
try {
proc = Runtime.getRuntime().exec("./ myServer.bat");
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Cool");
}
The last line is just there for me to see if the program actually ran in case the GUI doesn't open. Also, I've already tried many combinations of things to include in the area after ".exec". I've tried using a path like "~/user/workspace/ProjectServer/dist/myServer.bat" to no avail.
I also already know that .bat files are for windows, but I'm able to execute it in linux, so I don't know if that makes a difference. I also tried using a .sh file the same way and it didn't work.
Please bear in mind that I'm not that great at Java, but I had to use it for this particular program, so if your answers could be really descriptive that would be awesome.
Just take that line out of the bat file, and run it. Yo're making it too hard.
$ java -jar "NewServer.jar"
will work. The quotes aren't necessary, so
$ java -jar NewServer.jar
will work as well. If you want to have the equivalent of your bat file, create a file named, say, run_newserver containing that line. Change its mode to executable:
$ cat > run_newserver
java -jar NewServer.jar
^D
$ chmod a+x run_newserver
$ ./run_newserver
Ideally, since you shouldn't have scripts without comments, do this. In your favorite editor, create a file run_newserver containing
#!/usr/bin/env bash
java -jar NewServer.jar
and chmod that. The line with #! -- often called a "shebang line" -- is UNIX magic that lets you say what interpreter you want. The program env in usr/bin finds your program and runs it (needed because different systems put bash in different directories.)
You could even put explanatory comments in the file too.
I'm a little unclear why you want to use Runtime#exec to run it at all -- it seems you'll just need a shell script to start that program.
Why are you using Java to run a Batch file, that in turn runs a Java program? Why have Batch in the loop at all? Just put the jar in your classpath and call it directly.
Batch (.bat) files are only for Windows environment. So, Try using shell script
Process proc = Runtime.getRuntime().exec("myServer.sh");
Just open up terminal and do this
vi /dir/to/exec/exec.sh
tap "i" and write this
#!/bin/sh
java -jar "NewServer.jar"
or if you want to run it in the background
#!/bin/sh
java -jar "NewServer.jar" & > /tmp/JavaServer.log
hit esc and type ":wq" and you have saved the file.
type this into the terminal
chmod +x /dir/to/exec/exec.sh
this give executable privileges and then you should run the file like
sh /dir/to/exec/exec.sh
Process is only initialized by your first call. You need to run:
proc.waitfor();
to get it to actually run your app.

Categories

Resources