Shell Script Run Java Jar Service
i have tried this one, but not succeed Solution: Add an entry to your MANIFEST.MF (under META-INF directory) for the attribute and say run jar file with different jre For best practices I allways define var like this: So after a little refactoring I think that the script could be: if you want to see the command executed use the echo command: Solution 3: When you are assigning value to your variable, shell is treating special character as key character(not as value).
Shell Script Run Java Jar Service
I want to create a shell script to run java service. The actual command is
java -Dorg.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory -Dlogback.configurationFile=conf/web/logback.xml -jar vertx-web-0.1-****.jar -conf conf/web/conf.json -cluster -cp conf/web:conf/web/lib/slf4j-api-1.7.12.jar:conf/web/lib/logback-classic-1.1.2.jar:conf/web/lib/logback-core-1.1.2.jar
it works if I execute it just like that. But it’s gonna be inaffective and messy. I decided to create a shell script :
#!/bin/sh app_name=vertx-web-0.1-****.jar #::=== logger logger_name=logback #::=== config config_base=conf/web config_app=conf.json config_log=logback.xml #::=== arguments args_factory_logger=org.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory args_classpath=conf/web:conf/web/lib/slf4j-api-1.7.12.jar:conf/web/lib/logback-classic-1.1.2.jar:conf/web/lib/logback-core-1.1.2.jar args_other=-cluster #::=== execute java -D$args_factory_logger -D$logger_name.configurationFile=$config_base/$config_log -jar $app_name -conf $config_base/$config_app $args_other -cp $args_classpath
But it doesn’t work and the response
: command not found : command not found : command not found : command not found : command not found : command not found Error: Unable to access jarfile vertx-web-0.1-****.jar : command not found
Can you give me any clue or solution? Thank you
To launch the java app as a service I recommend you take a look at JSVC project. This is really easy to implement and that takes care of many things for you.
I think that you need to delcare var in the safe way. For best practices I allways define var like this:
So after a little refactoring I think that the script could be:
#!/bin/sh app_name="vertx-web-0.1-****.jar" #::=== logger logger_name="logback" #::=== config config_base="conf/web" config_app="conf.json" config_log="logback.xml" #::=== arguments args_factory_logger="org.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory" args_classpath="conf/web:conf/web/lib/slf4j-api-1.7.12.jar:conf/web/lib/logback-classic-1.1.2.jar:conf/web/lib/logback-core-1.1.2.jar" args_other="-cluster" #::=== execute java -D$ -D$logger_name.configurationFile=$config_base/$config_log -jar $app_name -conf $config_base/$config_app $ -cp $
if you want to see the command executed use the echo command :
echo "java -D$ -D$logger_name.configurationFile=$config_base/$config_log -jar $app_name -conf $config_base/$config_app $ -cp $"
When you are assigning value to your variable, shell is treating special character as key character(not as value). thus trying to execute the special char «:».
What you need is to use quotes , either strong quote or week quote depending on your need.
In your case i suggest strong quotes.
so you command should be like :
args_classpath='conf/web:conf/web/lib/slf4j-api-1.7.12.jar:conf/web/lib/logback-classic-1.1.2.jar:conf/web/lib/logback-core-1.1.2.jar'
Run java using custom JRE
im trying to run my jar file using a .bat file with a custom bundled jre.
i dont know what should i write to bat file.
[PATH_TO_YOUR_JRE]\java[.exe|.bat] [OPTIONS] [CLASS] [ARGUMENTS]
just qualify your version of java’s path
c:\mybundle\bin\java -jar myjar.jar
Is the bat in the same directory as the JRE distribution folder?
Have you tried ./myjre/bin/java.exe -jar myjar.jar ?
Run jar file in shell script Code Example, start file jar with java code. execute .jar windows 10. jar run other jar. java run file jar. run jar file by. its possible to execute a jar file inside a jar file. jhow to run jar file. how to run a jar file on windows 11. compiling java files with jar files.
Running Jar file with embedded JRE using batch script
If i want to embed JRE with my application, i have this directory structure .
+---jre | +---bin RunMyApp.bat myApp.jar
i have added this statement in RunMyApp.bat to access the java.exe
Then how to run the myApp.jar, using RunMyApp.bat script?
i have tried this one, but not succeed
jre\bin\java.exe -jar "%cd%\..\..\myApp.jar"
Add an entry to your MANIFEST.MF (under META-INF directory ) for the Main-Class attribute and say jre\bin\java.exe -jar
How to deliver my Java application with a particular JRE?, If you have the jre installed on a target platform, say Windows 64 bit, simply copy everything under the jre folder in your Java install and place it in your distro. Then create a batch file to point to your local jre instead of the system one. create a .bat file with the following line in it jre-win-1.7.0_04\bin\java.exe -jar …
Run jar specific jre
run jar file with different jre
First, go to the location of the java installation file. By default, this is: C:\Program Files\Java\. Open the folder of the java version you want and go to the bin folder. Copy the directory your at then go to your command line. Syntax: (%PATH% is path you copied): "%PATH%/java.exe" [javaArgs] [filePath] [yourCodeArgs]
Java — Shell script to run jar of any version, How can I make a shell script that will execute a jar file, regardless of its version? Say I have a file: my-jar-file-1.0.0-SNAPSHOT-executable.jar. I want a shell script that will do something along the lines of #!/bin/bash java -some -other -options -jar my-jar-file-1.0.0-SNAPSHOT-executable.jar But it should execute any …
MathOliveira / Creating your start and stop script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
With this script you can turn your jar file into a service on linux. |
Change the MATH constant to the name of your application and make sure you are correctly specifying |
the locationsof the files used in the variables. |
For the correct operation of the script install it in the directory init.d and release its access as below: |
chmod 755 *your_script* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
#! /bin/bash |
# ## BEGIN INIT INFO |
# Provides: MATH |
# Required-Start: $java $pgrep |
# Required-Stop: $java $pgrep |
# Short-Description: Start and stop MATH service. |
# Description: MATH it’s a java appplication example |
# Date-Creation: 2018-09-26 |
# Date-Last-Modification: 2018-09-26 |
# Author: Matheus Sant»Anna de Oliveira |
# ## END INIT INFO |
# Variables |
APPDIR=/opt/MATH |
PGREP=/usr/bin/pgrep |
JAVA=/usr/bin/java |
MATHJARFILE=/MATH.jar |
ZERO=0 |
OKMSG=OK |
MATHPORT=3131 |
# Colors |
RED= ‘ \033[1;31m ‘ |
GREEN= ‘ \033[1;32m ‘ |
YELLOW= ‘ \033[1;33m ‘ |
BLUE= ‘ \033[1;34m ‘ |
NC= ‘ \033[0m ‘ # No Color |
# Start the MATH |
start () |
echo » « |
echo » Starting MATH. « |
# Verify if the service is running |
$PGREP -f MATH > /dev/null |
VERIFIER= $? |
if [ $ZERO -eq $VERIFIER ] |
then |
echo -e » The service is $ already $ running « |
else |
# Run the jar file MATH service |
$JAVA -jar » -Dfile.encoding=UTF-8 » $APPDIR$MATHJARFILE q > /dev/null 2>&1 & |
# sleep time before the service verification |
sleep 3 |
# Verify if the service is running |
$PGREP -f MATH > /dev/null |
VERIFIER= $? |
if [ $ZERO -eq $VERIFIER ] |
then |
echo -e » Service was $ successfully $ started « |
else |
echo -e » $ Failed $ to start service « |
fi |
fi |
echo |
> |
# Stop the MATH |
stop () |
echo » « |
echo » Stopping MATH. « |
# Verify if the service is running |
$PGREP -f MATH > /dev/null |
VERIFIER= $? |
if [ $ZERO -eq $VERIFIER ] |
then |
# Kill the pid of java with the service name |
kill -9 $( $PGREP -f MATH ) |
# Sleep time before the service verification |
sleep 3 |
# Verify if the service is running |
$PGREP -f MATH > /dev/null |
VERIFIER= $? |
if [ $ZERO -eq $VERIFIER ] |
then |
echo -e » $ Failed $ to stop service « |
else |
echo -e » Service was $ successfully $ stopped « |
fi |
else |
echo -e » The service is $ already $ stopped « |
fi |
echo |
> |
# Verify the status of MATH |
status () |
echo » « |
echo » Checking status of MATH. « |
img |
# Verify if the service is running |
$PGREP -f MATH > /dev/null |
VERIFIER= $? |
if [ $ZERO -eq $VERIFIER ] |
then |
echo -e » Service is $ running $ « |
else |
echo -e » Service is $ stopped $ « |
fi |
echo |
> |
# Main logic from script |
case » $1 » in |
start) |
start |
;; |
stop) |
stop |
;; |
status) |
status |
;; |
restart|reload) |
stop |
start |
;; |
* ) |
echo » $ Invalid arguments $ « |
echo » Usages: $0 $ < start | stop | status | restart | reload >$ « |
exit 1 |
esac |
exit 0 |
shell script to run jar file
posted 3 years ago
I have jar file named as example.jar that I can execute it in CMD in windows by java -jar example.jar. I want to write a shell script that I can execute the example.jar file. I wrote this script
Saloon Keeper
posted 3 years ago
I don’t think you’re trying to run the same JAR. If that message means what I think it does, the file «example.jar» doesn’t have a proper manifest file in it, just one with a stub that looks like this:
The text «name of the main class» needs to be replaced with the fully-qualified class name containing the main() method. (package and classname).
The secret of how to be miserable is to constantly expect things are going to happen the way that they are «supposed» to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there’s being a willfully-blind idiot, which virtually guarantees it.
Marshal
posted 3 years ago
- 1
Saloon Keeper
posted 3 years ago
Campbell Ritchie wrote: Also, surely your .sh file should start with #! rather than ! shouldn’t it?
Oh yes. A simple «!» is an attempt to use command history. The «#!» is called «shebang», which comes from «shell-bang», where «bang» is the nickname for an exclamation point, like «splat» is for an asterisk.
When you start a script file with a shebang, the first line of that script will be executed as a command. /bin/sh for generic shell, /bin/bash for the Bash shell, /bin/zsh for zshell, and non-shell commands like /usr/bin/python or /usr/bin/perl.
And yes, a 1-line script could look like this:
The secret of how to be miserable is to constantly expect things are going to happen the way that they are «supposed» to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there’s being a willfully-blind idiot, which virtually guarantees it.