3

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.
3
  • Does the filename get passed to the java program, or do you have to enter it into the program by hand? Commented Jun 27, 2016 at 23:36
  • No the file name is not passed into the program as an argument, but one has to enter it by hand and then enter quit to quit the program. Commented Jun 27, 2016 at 23:41
  • There. I got you something that should work for you. Feel free to comment on it, suggest improvements, and accept my answer if you use it. Thanks. Commented Jun 27, 2016 at 23:54

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@sjsam: Thanks. (Sadly, some of us aren't as aware of all the differences. Thanks for the comment--it's appreciated.)
2

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

1 Comment

Could you explain the second way? "2nd by running java class from terminal" I am struggling to find any documentation on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.