Execute a .jar file using shell script - java

I want to execute a jar file of DOMO CLI from a shell script. The jar file itself has some functions which I want to call after I call the main jar file. The problem which I am facing is that after it executes the jar file, I am not able to pass the additional commands to execute inside that jar through a shell script. It just stops after calling jar and doesn't take further commands. Can anyone please help? Below is the code I am calling from a shell script.
java -jar XX.jar
The commands are as below which follow the above jar. So once we enter into the above jar we have to execute the below commands one after the other. I am not sure how to achieve this through a shell script.
connect -s X.domo.com -t Ysssss
upload-dataset -a -i dhdhdhdh -f /prehdfs/dev/comres/herm/data/yyyy.csv

Did you try using pipes and inputs.
When you execute above it runs it under a child shell.
You may try below format if not tried already
$ (echo "connect -s X.domo.com -t Ysssss" && cat) | java -jar XX.jar

If you can reference a file in your use case, you could put your commands in a file.
File: list_my_datasets.domo
connect -t ... -s ...
list-dataset
quit
then run the command:
java -jar domoUtil.jar -script list_my_datasets.domo > datasets
I wanted the data from it so I piped to a file (where I had to grep what I wanted), but you would omit that I believe, unless it has some output you'd want to check. I haven't tested with the upload command, but I would hope any commands substituted or added to the example work similarly.
Domo docs on scripting

Related

Run jar on remote machine as background process over ssh

I want to run a jar as a background process on a remote machine over ssh connection. There is bash script on remote machine to execute jar
#!/bin/sh
export JAVA_HOME=/location/of/java/
export PATH=$JAVA_HOME/bin:$PATH
nohup java -jar jar_name.jar config.properties &
If I execute the above script directly from remote machine(sudo ./start_script.sh), jar is started as background process and stdout is directed to nohup.out in the same folder as jar. But when I run script from local machine : ssh vm_name 'sudo ./start_script.sh', the process starts up. but it blocks and the output is directed to local terminal.
is there way to achieve this?
EDIT: I need to run script as root and also pass parameters to script, added placeholder path for JAVA_HOME to avoid confusion
You need to tell the ssh to connect as a terminal.
ssh vm_name -t 'sudo ./start_script.sh'
It's likely recognizing that you are not on as a terminal and altering behavior accordingly.
Same issue related here:
https://serverfault.com/questions/955268/what-is-the-difference-between-running-a-command-in-ssh-shell-manually-vs-runnin
Try
$ ssh user#host bash -l ./build.sh

Shell script to launch Java app won't execute

I am using debian and xterm.
I have created a file 'run.sh' containing the following:
java -cp bin Main
read -n1 -r ip "Press any key to continue..." key
In Properties window I gave it permission to run as program.
Double clicking the file does nothing. Right click 'Execute' does nothing. Open-with UXTerm does nothing.
If I open a terminal in the same directory and type
java -cp bin Main
then it will run, but the shell script file never works.
What am I doing wrong here?
Your shell script file doesn't seem to have a shebang line,
#!/usr/bin/env bash
java -cp bin Main
read -n1 -r ip "Press any key to continue..." key
and make sure it has a execute permissions
chmod a+x <script_file>
You need to add a shebang line at the top of your file: #!/usr/bin/bash. This tells the operating system that the file is in fact a bash executable rather than a normal file. Alternatively, execute the script by entering bash run.sh from the command line.

Command works in terminal but not in script?

I have the following script which won't work when executed as a script, but does work when the exact same commands are entered into the terminal:
#! /bin/sh
cd ~/Desktop/Example/
javac Generator.java
The error message is:
my_script.sh 3: my_script.sh: javac: not found
The above script is named my_script.sh and I execute it from the terminal using:
sh my_script.sh
when I do
echo $SHELL
in the terminal I get:
/bin/bash
Add jmlc to your path and run the script again.
To check: Open a new shell and type 'jmc'.
Another way to get your script working is to specify the full path in your script. Replace 'jmlc' with '/full_path_here/jmlc'.
Also make sure that any other commands in jmlc script are also available in the path.
You can also made jmlc available by exporting its PATH:
#! /bin/sh
export jmlc_bin=FULL_PATH_TO_JMLC
cd ~/Desktop/Example/
$jmlc_bin Generator.java
Navigate to the directory where your single line commands were working and save your script in that directory.
then execute
./my_script.sh

ArrayIndexOutOfBounds Exception on executing linux sh file

I have a program in java which takes 0'th aargument as file location like
File f = new File(args[0]);
so when i execute it using a windows batch(.bat) file it works correctly .
but when i execute the same using a linux shell file(.sh) in linux i get ArrayIndexOutOfBoundsException.
WINDOWS BATCH FILE :
#echo off
for /f %%i in ("%0") do set scriptpath=%%~dpi
set cp=%scriptpath%/../lib/*.jar;
java -classpath %cp% com.synchronizer.main.MYSynchronizer %scriptpath% "%1" "%2"
LINUX SH FILE:
export JAVA_HOME=/usr/local/java
PATH=/usr/local/java/bin:${PATH}
THE_CLASSPATH=
for i in `ls ../lib/*.jar`
do
THE_CLASSPATH=${THE_CLASSPATH}:${i}
done
java -cp ".:${THE_CLASSPATH}" \
com.synchronizer.main.MYSynchronizer
please help!
It looks like a problem in script (no arguments are passed to the Java program).
You can consider to debug the script like this: debugging scripts
Hope this helps
Your shell script is not passing any parameters:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer
Try:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer "$1" "$2"
As stated above, your Linux shell script is not sending any arguments to the Java program that you are trying to start.
And, adding to that, you are not showing us how you run the Linux shell script. If no argument is given on the command line when you start the shell script, no arguments can be passed to your Java application from the shell script.
If you want to see the actual command that is going to be run by your shell script, you can always put "echo" in front of a line and see what all variables are expanded to. This is a simple way to debug shell scripts.

Creating a shell script to run Java program

I used a shell script to run a Java class.
My script contains
#!/bin/sh
java -jar jobs/job.jar
These are my failed attempts to run it.
[root#]#sh testapp.sh
Unable to access jarfile jobs/job.jar
if I just do this at the command line it works fine
[root#]#java -jar jobs/job.jar
thanks.
The best way is to get the current dirname and get in there with this:
#!/bin/sh
cd `dirname "$0"`
java -jar ./job/job.jar
Use the absolute path to your JAR file, e. g. /root/jobs/job.jar.

Categories