Can't access Java from shell script - linux

I am trying to execute a jar file from a bash script, but for whatever reason, java subcommands are not working within the shell script. Attached is an example of a simple script, and the error it returns. The same error is outputted when I use any subcommand for java.
What do I need to fix?
#!/bin/sh
java -version

Related

How to execute a script with using a keyword

I have a requirement, where I have my linux script which does my work. But I want to use a simple keyword to execute my script file and get the output.
example:
sudo save
The above command should execute my script file which is saved as abc.sh
How to do it in linux or mac. Please help

Jar file not executing in ksh cygwin

My Java team provided me a jar file and below command to execute in Cygwin environment:
java -DmoduleName="GBL" -jar G:/CSFBApps/planning/bin/planning_aura.jar G:/CSFBApps/planning/bin/resources
This command when I trigger from command prompt working fine but when I put the same in sample KSH script:
java -DmoduleName="GBL" -jar G:/CSFBApps/planning/bin/planning_aura.jar G:/CSFBApps/planning/bin/resources
echo $?
it just executes and showing 1 (which i think not executed).
So i believe KSH not invoking jar, please help me.
Checkout other posts showing how to use cygpath with java in cygwin

Why does a bash script require an execute bit if a windows batch script can just be executed?

Yesterday I ran into the git execute bit bash script quirk - the one that requires:
git update-index --add --chmod=+x scriptname.sh
and it seemed strange to me that it was even possible to get stuck in this situation. (Ie having created a script file that you don't have permission to run).
If I have created a shell script - surely I can run it under the permissions of the shell execute permissions. Why would it need it's own execute permission bit?
My question is: Why does a bash script require an execute bit if a windows batch script can just be executed?
To run a script you have two options in unix like systems. First Option is to use a direct interpreter call with the script as parameter.
# run a bash script
bash test.sh
# run a python scripts
python test.py
The second option is mark your file as executable, with the execute bit and after a call like this ...
# sample bash
./test.sh
# sample python
./test.py
... your system tries to find the right interpreter for you. For this the first line 'shebang' of the script is used.
Bash example:
#!/bin/bash
# points to the installed bash interpreter - bash example
Python example:
#!/usr/bin/python
# points to the installed python interpreter
To your question windows only use the file extension to detect a executable file.
Well, Linux is not Windows. Linux/Unix file systems support the executable bit to distinguish executable from pure data files, and to control exec permissions for user|group|others. You can still run the script if you prefix it with the name of the shell/binary you want to start it with, but if you want to do ./scriptname.sh or execute it from the path it needs to be flagged as executable for you as the onwer|a group member|some other user, and for scripts usually the shebang in the first line that defines the interpreter to start the script with: #!/bin/bash.

No output when running jar from shell script

I am running a jar-file in a shell script calling hierarchy. I want to redirect the output from executing the jar file to my logfile "loga.log" but this does only work when I run this script with "sh"-command.
My calling hierarchy basically looks like this:
Script A calls script B with:
/path/to/script/scriptb.sh &> /path/to/logs/loga.log
Script B calls script C with:
/path/to/script/scriptc.sh > /path/to/logs/logb.log
Script C executes jar-file with:
java -Xms512m -Xmx1280m -Djava.awt.headless=true -jar /path/to/jar/thejar.jar > /path/to/logs/stdout.log #2>/path/to/logs/stderr.log
The first script is started by command line with:
sh /path/to/scripts/scripta.sh
and then I get written my output as wanted to the loga.log-file. When I start the script via:
/path/to/scripts/scripta.sh
this doesn't work and the output is displayed in the console. Unfortunately I am not able to use the sh command because these scripts must be triggered by a UC4-job system which calls this script without using sh. Calling the second script using "sh" inside the first one doesn't work eigther.
How can I edit the scripts to be able to log all output to the specified logfile when running the first one via the above command (without sh)?
Thanks in advance,
Martin

Running a command in shell script

I have a shell script file (run.sh) that contains the following:
#!/bin/bash
%JAVA_HOME%/bin/java -jar umar.jar
when i try to run it (./run.sh), it gives me following:
umar/bin/run.sh: line 1: fg: no job control
However if I run same command directly on shell, it works perfectly.
What's wrong with the script file?
Thanks
%foo% is not how you do command substitution in a bourne/BASH shell script. I assume you're running this from a Windows command line, which is why it works when you run it directly. Try using proper bourne syntax:
${JAVA_HOME}/bin/java -jar umar.jar
Try turning on monitor mode
set -m
%JAVA_HOME% will substitute a Windows environment variable and is appropriate in a .bat file.
Try the following shell script which should work on most UNIX like systems.
#!/bin/bash
$JAVA_HOME/bin/java -jar umar.jar

Resources