i've the following problem, these is my shell script:
#!/bin/bash
CONFIG_FOLDER=./config
JAVA_HOME=/home/lorenzo/Downloads/jdk1.7.0_25/bin
CHARSET=utf8
$JAVA_HOME/java -DconfigFolder=$CONFIG_FOLDER -jar com.lorenzo.myapp.jar
When I run the script I get the following error:
/java: not foundmyscript.sh: 5: myscritp.sh: /home/lorenzo/Downloads/jdk1.7.0_25/bin
Any idea?
P.S myscript.sh is the posted script.
EDIT:
I've found a partial solution:
#!/bin/bash
CONFIG_FOLDER=./config
export PATH=$PATH:/home/user/jdk1.7/bin
java -DconfigFolder=$CONFIG_FOLDER -jar com.lorenzo.myapp.jar
But now, I've another problem: the value of $CONFIG_FOLDER is empty when i run the script. Why?
Finally i've found solution! I wrote the script under Windows operative system. If i I open the script under Linux with vim text editor the script appear in this way:
#!/bin/bash^M
CONFIG_FOLDER=./config^M
JAVA_HOME=/home/lorenzo/Downloads/jdk1.7.0_25/bin^M
CHARSET=utf8^M
$JAVA_HOME/java -DconfigFolder=$CONFIG_FOLDER -jar com.lorenzo.myapp.jar^M
The script is dirty! After deleted all ^M the script worked!
Related
I made a java project.
The project is....output log message and system.out.println message. just simple.
So I changed into a jar file(the name is LinuxSample.jar).
and I wrote a shell script to run this jar file.
Look at this shell script. (speakee is package name and PrintLinux is main class name)
#!bin/bash
CLASSPATH=/home/tangooc/TANGOOC/test/libs/*
CLASSPATH="${CLASSPATH};/home/tangooc/TANGOOC/test/linux/LinuxSample.jar"
java speakee.PrintLinux
this jar file and this shell script work in Window.
but linux didn't work. I don't know why
this is error message.
Could not find or load main class
Hi Best way to run a java application is to set CLASS_PATH and PATH variable first. If your current jar file depends on external jar files you will face lots of problem. Better set your path variable like below and run the application:-
#!/usr/bin/ksh
export PATH=/usr/java/bin:$PATH
# =/usr/java/bin is your java bin folder
#set environment variable CP with all the jar libraries
CP=/home/flussi/xmlEncoder/encoder.jar
CP=${CP}:/other/jar/somejar.jar
java -Xmx256M -classpath "$CP" "com.myproj.Example"
I made it
I changed the shell script.
CLASSPATH=/home/tangooc/TANGOOC/test/client/LinuxSample.jar
LIB_TOTAL=/home/tangooc/TANGOOC/test/libs/*
echo ${LIB_TOTAL}
echo ${CLASSPATH}
java -cp ${LIB_TOTAL}:${CLASSPATH} speakee.PrintLinux
also there is another way.
CLASSPATH=/home/tangooc/TANGOOC/test/client/LinuxSample.jar
CLASSPATH=${CLASSPATH}:/home/tangooc/TANGOOC/test/libs/*
echo ${CLASSPATH}
java -cp ${CLASSPATH} speakee.PrintLinux
If the someone like me change the shell script.
and check a line, a line, a line...
I have any problems to configure Fluentd; I want call in_exec plugin to launch Linux bash script that call a jar but it doesn't work.
My code:
<source>
#type exec
format none
tag none
command sh /var/tmp/script.sh
run_interval 5m
</source>
And my script is:
#!/bin/bash
java -jar example.jar
I don't understad where I wrong; if I change script (for example to create a file) all it's OK but if I use java command it doesn't work.
Please help me!
Thanks
Try to update your script with the absolute path to the jar in the sh file. Otherwise, there may be a problem when executing script in different locations and it will fail to find the jar.
#!/bin/bash
java -jar /hom/{User}/{Path to Jar}example.jar
I am currently using a linux system to execute a specific command that translates into another java command (java - jar)
For example:
when i try execute /usr/bin/a in terminal, it will read the /usr/bin/a command and translates into 'java -jar' command
I do not want to execute 'java -jar' command directly and i would to specify a full path in executing the command so is there a possible way to achieve this without using a script like /.sh?
Things that i have attempted:
i have tried using the alias command in bashrc files
for example in bashrc file:
alias /usr/bin/a='java - jar'
but when i try to source the bashrc files, it gives me an invalid alias.
I know i can use a /.sh script to execute the command but that is not my intention to do it.
Have you tried to write a bash script with java -jar and execute it ?
You test.sh file will contain:
java -jar test.jar
And after that you can run
./test.sh
And offcourse you can put to bin directory or create an alias for that bash file
I used to use Debian's alternatives system to set 'global env' like java, javac, javap but I've read about the disadvantages.
So I added
export JAVA_HOME=/opt/jdk/java
export PATH=$JAVA_HOME/bin:$PATH
to my ~/.bashrc and when I open my terminal I can use the commands as expected but my most of my shell scripts doesn't work anymore.
As you can see in the picture below they check if $JAVA_HOME exists and executes the following command which does nothing. When I enter $JAVA_HOME/bin/java -version it works correctly. If I start the script in a terminal it works, too.
So it seems that #!/bin/sh doesn't source .bashrc? Changing it to #!/bin/bash doesn't solve the problem.
I tried to add the export commands to /etc/profile but this doesn't seem to get sourced at startup/login.
Does anyone have an idea or keywords? I think the solution is quite simple but at the moment I'm stuck.
Thank you in advance!
UPDATE:
Starting the script in the bash terminal with ./something.sh works fine. Right click and execute or 'Open with bash' (XFCE4 context menu) does nothing.
Bash loads .bashrc only when the shell is interactive and non-login. In your case the shell is not interactive, so .bashrc is not loaded.
.bashrc contains a check that prevents it from executing if the shell is not interactive. Usually the first thing .bashrc does is:
case $- in
*i*) ;;
*) return;;
esac
This will prevent you from calling source .bashrc from your script.
The script should inherit from your parent shell, so you should be getting all variable that have been exported before you ran the script.
Also, the preferred shebang is #!/usr/bin/env bash which is more portable.
So in your case:
Open a terminal window. That will load .bashrc, but just to make sure, run . .bashrc and then echo $JAVA_HOME to verify that the variable was set correctly.
Then your script will simply be:
#!/usr/bin/env bash
java -jar <whatever>
If you have some other script related variable that you want to set, you can do that by sourcing a "settings" script:
#!/usr/bin/env bash
source ~/settings.sh
echo $SOME_VAR_SET_IN_SETTINGS_SH
Adding additional folders to your PATH variable in XFCE4 with LightDM will not work as expected! Shell scripts that use these additional commands and are started from the graphical environment will fail because the can't find the command. Logging the PATH variable while starting one of these scripts shows that the PATH variable is overwritten with the very default one. Why? Because LightDM hardcodes the PATH variable and overwrites it for the graphical environment. Screw you! Look here!
Source: https://ljwo.wordpress.com/2014/02/02/global-path-in-debian-wheezy-xfce/
Disable it or use another DM.
I have a java program that I would like to be able to run from anywhere on my machine. I would like to run it from my Cygwin command prompt. I've made scripts to call the java program. I added the location of the java program to the classpath, and the scripts work when I run them from the java program's directory. However, when I try to run from any other directory, I get:
java.lang.NoClassDefFoundError: commandprogram/CommandProgram
This is my script:
#!/bin/sh
CWD=`dirname "$0"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Changing the java line to the following:
java -cp "$CWD/classes;$CWD/classes/commandprogram;$CWD/lib/AJarFile.jar" CommandProgram
produces the same results.
add your directory to classpath example:
java -classpath commandprogram CommandProgram
or
java -classpath directory_to_program Program
After trying just about everything I could think of, I echoed out the command and saw that there was mixing of Cygwin paths and Windows paths. The solution was to change the script to:
#!/bin/sh
CWD=`dirname "$0"`
CWD=`cygpath -w "$CWD"`
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram/CommandProgram
Then CWD changed to "C:\Program Files\..." instead of "/cygdrive/c/Program\ Files/..."
I had previously encountered this problem and solved it with the cygpath -w solution, but then changed my script slightly and didn't notice that the path problem came back.
you have to use a dot to separate packages, not a slash.
java -cp "$CWD/classes;$CWD/lib/AJarFile.jar" commandprogram.CommandProgram
The usual way of running a java file is to save it in the Java/Bin folder and Run cmd
C:\Program Files\Java\jdk1.7.0_05\bin> javac filename.java && java classname
If you save the file in different directory such as D:, you can use the following on the cmd prompt:
D:\Project java> set path=%path%;C:Program Files\Java\jdk1.7.0_05\bin