2

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.
  • Does the filename get passed to the java program, or do you have to enter it into the program by hand? – J. Allan Jun 27 '16 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. –  Jun 27 '16 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. – J. Allan Jun 27 '16 at 23:54

2 Answers2

3

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.

J. Allan
  • 1,418
  • 1
  • 12
  • 23
  • 1
    @sjsam: Thanks. (Sadly, some of us aren't as aware of all the differences. Thanks for the comment--it's appreciated.) – J. Allan Jun 28 '16 at 01:22
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
gofr1
  • 15,741
  • 11
  • 42
  • 52
vikas ray
  • 191
  • 2
  • 8
  • Could you explain the second way? "2nd by running java class from terminal" I am struggling to find any documentation on this. – user313 Aug 24 '22 at 20:50