Create a Shell Script to Run a Java Program on Linux

Create a shell script to run a Java program on Linux

create a file named "sync" in /usr/bin containing the following:

java -jar {PATH TO JARFILE} $1 $2

Replace {PATH TO JARFILE} with the path to the jarfile

Make the file executable by typing chmod +x sync while in /usr/bin

How to create a shell script which can create a .java file on executing?

Here-docs will expand variables, so

NAME=$1
cat << EOF > $NAME.java
class $NAME{
public static void main(String[] args){
for(int i=0;i<10;i++){
System.out.println("Hello World !!!");
}
}
EOF

Running a java program from shell script with redirection

It's a strange and non-canonical thing to do, but yes:

#!/usr/bin/bash
javac -O Main.java uandf.java node.java
java -cp . Main "$(cat)"

Normally you would instead have your Java program read from System.in (as if from the keyboard), and just used:

#!/usr/bin/bash
javac -O Main.java uandf.java node.java
java -cp . Main

When running ./shell_script.sh < filename, reading from System.in via Scanner or BufferedReader would then return data from filename.

Writing a bash script to run a java program

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.

Creating a shell script to run Java program

Use the absolute path to your JAR file, e. g. /root/jobs/job.jar.

shell script to run java cmd in loop and start it in parallel with input parameter

There are a few bugs here:

1) $1 will only contain the first parameter. You will need $* to contain more than one. (And given that you want the variable to contain multiple ports, it would then be more helpful to call the variable PORTS.)

2) You cannot have the whitespace around the = in a variable assignment in bash.

3) You are looping over i but not using that variable inside the loop. Where you have -Dserver.port="$PORT" you should instead use your loop variable i.

4) You are missing a line continuation character \ at the end of the java ... line (ensure that there is no whitespace after it).

5) The command separator ; at the end of the first line is redundant (although it does not actually harm).

6) Where you are testing for wrong usage, the script will issue the warning but carry on regardless. You need to put an exit statement there. It is good practice to give a non-zero exit value in the event of a failure, so exit 1 is suggested here.

Putting these together:

#!/bin/bash

PORTS=$*

if [ $# -eq 0 ]
then
echo "No arguments supplied"
exit 1
fi

for i in $PORTS
do
java -DpropertySource=~/Downloads/app.properties -jar APP.jar \
-Dserver.port="$i" &
done

Regarding the &, it will launch the command in the background, so that execution of the script will continue (including then reaching the end and exiting) while the command that was launched may still be running. So yes, your java instances listening on the different ports will then be running in parallel.



Related Topics



Leave a reply



Submit