Pass parameters to bash file in the middle of a java code - java

I have a Java code running by the following command line
java -jar CPM-1.0-SNAPSHOT-jar-with-dependencies.jar Example arg[0] arg[1] arg[2] arg[3]
clearly I need to pass 4 parameters.
In the middle of my Java code, I should call a C++ code using the following bash file.
#!/bin/bash
/home/CPM/Codes/source/GcEqClass arg[0]
arg[0] in the bash file is same as arg[0] in Java command line and the main reason to have arg[0] in command line is to use it in bash script. My question, how can I pass arg[0] from the command line to the bash script without editing the bash file manually?

Your bash file should be:
#!/bin/bash
/home/CPM/Codes/source/GcEqClass "$#"
"$#" is basically equivalent to the the argument array in java, and similar to "$1" "$2" "$3" ..
From Java, you can then do:
Runtime.getRuntime().exec(new String[] {
"/yourpath/toyour/script", arg[0]
});

write the script:
#!/bin/bash
java -jar CPM-1.0-SNAPSHOT-jar-with-dependencies.jar Example $1 $2 $3 $4
save it as say scritp.sh, then run it :
./script.sh 1 2 3 4

Related

How to tell whether current bash script was invoked from call script

I have a script that ideally will be used in two different ways. One, run stand-alone from command line. Two invoke from a service script located in /etc/init.d.
I would like the script (call it run_app.sh) to work as follows:
#/bin/bash
# this is run_app.sh. It should be able to be run stand-alone or called from another script
if [ invoked by a calling script ] then
java -cp . -jar blah.jar
else
nohup java -cp . -jar blah.jar 2>&1 &
, so its the "invoked by a calling script" that I would need help with. Thank you.
If you plan to launch your script either by issuing ./run_app.sh or the service. You can just use $0:
#!/bin/bash
# this is run_app.sh. It should be able to be run stand-alone or called from another script
this_script_name="run_app.sh"
if [ "$0" == "./${this_script_name}" ] then
java -cp . -jar blah.jar
else
nohup java -cp . -jar blah.jar 2>&1 &
Instead of trying to find out how the script was called, I suggest to use a command line argument.
Script app.sh
#/bin/bash
# this is run_app.sh. It should be able to be run stand-alone or called from another script
if [ "$1" = "--service" ] then
java -cp . -jar blah.jar
else
nohup java -cp . -jar blah.jar 2>&1 &
fi
Manually run the script as app.sh, from the calling script run app.sh --service.
If you need to pass additional command line arguments to your script, you might have to implement some better option parsing.
Note: Checking $0 may work under certain conditions, but may not work in some other cases. Trying to find out details about the parent processes is even more difficult and fragile.
Another note: Your java command line arguments -cp . -jar blah.jar depend on the current directory. To make sure this works in all cases, the script should cd into the correct directory before calling java. e.g. cd $(dirname "$0") if the script is located in the same directory as blah.jar.

How to get arguments from console in java when using a bash file?

I have a java project that I launch using a bash file :
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest
I launch my bash in my terminal (in ubuntu) :
firefrost#firefrost-PC:~/ProjetPOO3A$ bash test.sh banking.Account
In my main, I try to get arguments as :
public class RunTest {
public static void main(String[] args) {
System.out.println(args.length);
for(int i = 0; i < args.length; i++)
{
...
The problem is that args.length is 0 when it should be 1 because I passed the argument "banking.Account" in my console.
If I put the argument in the bash file :
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest banking.Account
my main recognize the argument and the System.out.println outputs 1.
Why is the argument in the console not taken into account?
There is no problem in Java code. Your parameters are not propagated to java command in shell script.
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest "$#"
The $# variable contains all parameters passed to shell script.
Command line arguments to a bash script can be accessed with $1, $2, etc. Here's a fuller explanation.
Try updating your script as follows:
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest $1
You forget to process the argument in the bash file.
You can access them by using $1, $2, $3, $... variables.
For example writing ./my.sh test test2 $1 will contain test and $2 test2
So change your script to
#!/usr/bin/env bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest $1
Add "$#" to pass the arguments to the program:
#!/bin/bash
java -classpath logger/bin:banking/bin:testframework/bin test.RunTest "$#"
Don't forget the quotes around $# so bash will "protect" each argument:
#
Expands to the positional parameters, starting from one. When the
expansion occurs within double quotes, each parameter expands to a
separate word. That is, "$#" is equivalent to "$1" "$2" ... If the
double-quoted expansion occurs within a word, the expansion of the
first parameter is joined with the beginning part of the original
word, and the expansion of the last parameter is joined with the last
part of the original word. When there are no positional parameters,
"$#" and $# expand to nothing (i.e., they are removed).
See bash man.

Solaris/UNIX passing "$*" to compiled java

I am having a strange issue with passing "$*" to a java compiled program. The program will not parse the variables when I pass it from the following command line:
/export/home/checkout>/tmp/jsnmp.sh -f noc2 -t 4,4 -x \"resdiag SilentDiag 1\",18
The "/tmp/jsnmp.sh" contains the following:
#!/bin/sh
$JAVA_HOME/bin/java -jar /export/home/checkout/jsnmp.jar $*
Now if I run this:
$JAVA_HOME/bin/java -jar /export/home/checkout/jsnmp.jar \
-f noc2 -t 4,4 -x "resdiag SilentDiag 1",18
Everything works.
Any ideas folks?
You probably want to maintain the quoting within the script, so use "$#".
This has nothing to do with Java or Solaris, this is purely shell stuff.
This is because after $* substitution arguments will get re-parsed and will become separate arguments. E.g. your java executable will see it as
-f noc2 -t 4,4 -x resdiag SilentDiag 1,18
or something like that.
Check out the following test code:
a.sh:
echo $1
echo $2
echo $3
./b.sh $*
b.sh:
echo b
echo $1
echo $2
echo $3
Running it will produce the following output:
$ ./a.sh "1 2" 3
1 2
3
b
1
2
3
See how it was 2 arguments for the first script and 3 params for the second.
Encompassing $* in double quotes won't help, because it will send all arguments as one argument.
The following should work:
#!/bin/sh
$JAVA_HOME/bin/java -jar /export/home/checkout/jsnmp.jar "$1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9"
You will have some max number of arguments though...

shell scripting: java program can't find property file when starting by sh

I am starting a java programm under Red Hat Enterprise Linux Server release 5 (Tikanga).
directory structure:
- bin ->sc.jar,start-sc.sh,sc-lib-all.jar
- conf->log4j-sc.properties,sc.properties
command to run the java programm (which is perfectly working):
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j sc.properties -jar sc.jar -config ../conf/sc.properties
if i put it into a shell script the java programm can't find the prop file anymore.
shell script (start-sc.sh) looks like:
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:../conf/log4j-sc.properties -jar sc.jar -config ../conf/sc.properties
i am a newbie on shell scripting any ideas what i am missing? thx!
i guess you started your shell script not from the bin directory, which the dir start-sc.sh belongs to.
to explain it clear, let's make an example.
say, your script is here:
/foo/bar/bin/start-sc.sh
if you start it under /foo/bar/bin/, it (the relative path) should work.
but if you start your script from /home/yourHome/someDir/ , the relative path will point to $PWD/../, which is /home/yourHome/
you could either in your script first cd /foo/bar/bin/ before you start the java app. or do something like:
a=`dirname $0`
if [ $a = '.' ];then
a=`pwd`
fi
cd $a
/usr/java/jdkxxxx/java .....
It sound fine to me, does this version work?
#!/bin/sh
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$(pwd)/../conf/log4j-sc.properties -jar sc.jar -config $(pwd)/../conf/sc.properties
Edit #1:
Try put the following before launching your program:
echo `pwd`
The output tells you where you are running your script, so you can check if it's the right path or not.
Edit #2:
Try this script
#!/bin/bash
LOG4JCONF="/absolute/path/to/the/log4j/conf/file"
SCCONF="/absolute/path/to/the/other/conf/file"
/usr/java/jdk1.6.0_37/bin/java -Xmx1024M -Dlog4j.configuration=file:$LOG4JCONF -jar sc.jar -config $SCCONF

Bash multi input Java tester

I am currently trying to make a bash testing script that will...
1) Go into many peoples folders
2) Compile their two java files
3) Run two quick tests for the the compiled results and send the output to a file to be saved in their folder
4) Take the results of those four result files, and dump them into one result file with a template at the top for me to input the results
... and I currently have most of this done. My only issue is that their program asks for a couple lines of input, for example...
Input num 1:
Input num 2:
Input num 3:
... and so on, and I am not sure how to get it to continue putting input into their program. Do I need an EOF after my hard coded input in my bash file?? Here is what I have so far...
#! /bin/bash
for i in $(find . -maxdepth 1 -type d)
do
pwd
pushd "$i"
pwd
if [ -f "First.java" ];
then
javac -cp . First.java
echo easyFirst.txt | java -cp . First - > easyFirstResult
echo hardFirst.txt | java -cp . First - > hardFirstResult
fi
if [ -f "Second.java" ];
then
javac -cp . Second.java
echo easySecond | java -cp . Second - > easySecondResult
echo hardSecond | java -cp . Second - > hardSecondResult
fi
printf "easyFirstResult\t: \hardFirstResult\t: \easySecondResult\t: \hardSecondResult\t: " > lab5grade.txt
popd
done
P.S. Everything is working besides the multi-line input, and I have two text files with my hard coded input to test the code.
Thanks!
I see commands like
echo easyFirst.txt | java -cp . First - > easyFirstResult
apparently supplying a line of input to the java programs; but echo commands like that don't transfer file contents, they merely copy text like "easyFirst.txt" to stdout. To pipe the contents of file easyFirst.txt into First, use a command like
java -cp . First - < easyFirst.txt > easyFirstResult
(Note, the above supposes classpath is ., class is First, and - is an unexplained command line argument to First.)

Categories

Resources