I want a bash script that reads the java arguments from a file and executes java with them
jvm_arguments=$(cat jvm-args)
exec java $jvm_arguments
The problem is that it does not work very well with this jvm-args file:
-Xms128m -Xmx512m -Dhostname=$(hostname)
The command hostname is not executed and replaced.
How can I get the hostname command executed? so that I get:
exec java -Xms128m -Xmx512m -Dhostname=MyMachine
Thanks.
Use eval to evaluate your runtime variables. Like this (not tested):
jvm_arguments=$(cat jvm-args)
eval java $jvm_arguments
Store the whole command in file, for example, run.sh
exec java -Xms128m -Xmx512m -Dhostname=$(hostname) ...
If you want current bash process to be terminated, call with . run.sh
Related
java -jar /home/scripts/relay.jar is working fine when I launch from command line. The command produces a file: relay.txt
In crontab
/usr/bin/java -jar /home/oneprovider/relay.jar
is not producing anything. I first had it without /usr/bin/ but then did which java and added absolute path with no luck. The jar file was originally written for windows but it works in Linux fine when launched from command line
What am I missing?
Agreed that the working directory is likely the problem. Can you write a shell script that wraps the java invocation and sets the working directory? Something like:
#!/bin/sh -e
cd /home/oneprovider
/usr/bin/java -jar /home/oneprovider/relay.jar
Then change the cron job to run the script instead. Remember to chmod it and make sure that the cron user can write to the directory if it isn't your personal crontab.
What would be a solution to rebooting a java application with linux instead of using
"sh run.sh"
in terminal whenever I want to reboot it? The run.sh contains this:
java -Xmx1024m -Xss2m -Dsun.java2d.noddraw=true -XX:+DisableExplicitGC -XX:+AggressiveOpts -XX:+UseAdaptiveGCBoundary -XX:MaxGCPauseMillis=500 -XX:SurvivorRatio=16 -XX:+UseParallelGC -classpath bin:data/libs/* com.runeown.Application
I want to restart it using terminal.
If you do not want to restart it using "sh run.sh" and instead you want to restart it using a command, you could use a bash alias to run the command.
here is the manual (http://www.ss64.com/bash/alias.html)
here is a relevant askubuntu thread about making a permanent alias (https://askubuntu.com/questions/17536/how-do-i-create-a-permanent-bash-alias)
An alias is essentially a shortcut for the terminal, for instance you had a dropbox folder located at ~/myfiles/1/2/dropbox and you did not want to type cd + path every time, you could make an alias:
alias cddropbox="cd ~/myfiles/1/2/dropbox"
I wanna re-run a jar file from its own (with some additional parameters). How can I do this?
I need the solution to be OS independent.
If I deciphered the question correctly, we are talking about command line interface arguments. For this there's plenty tutorials: http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html
The code is simple, like you said yourself:
if ("-server".equalsIgnoreCase(argv)) {
// we are server
} else if ("-client".equalsIgnoreCase(argv)) {
// we are client
}
Now, depending on how you want to execute you program from the OS, there's a number of ways:
$java -jar yourjar.jar -client
Or
$java -cp yourjar.jar com.your.program.Main -client
Same for the "-server".
To run them together, either run them from separate terminal windows (or cmd prompts). Or - if in Linux - you can use ampersand:
$java -jar yourjar.jar -client &
$java -jar yourjar.jar -server &
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.
-java -classpath<> <classname> in the ".bat" file to launch java test from cmd windows
how to do that using perl to launch java test from linux ?
Don't use perl. For such a simple job, a simple shell script will do:
#!/bin/sh
/path/to/java -classpath foo.jar:bar.jar:. classname
Make the file executable with chmod +x filename and execute it with ./filename
A similar approach using the -jar option is possible. Additionally, you can forward any command line parameters using the special parameter #.
#!/bin/sh
/path/to/java -jar foo.jar "${#}"