简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM