Error executing ant :-bash: ant: command not found - java

I am trying to deploy my application in a Linux box, I have a file called setAppPath.sh file as:
#!/bin/sh
APP_HOME=`pwd`
ANT_HOME=$APP_HOME/lib/ant
echo $ANT_HOME
PATH=$ANT_HOME/bin:$APP_HOME/scripts/unix:$PATH
echo $PATH
chmod +x $ANT_HOME/bin/ant
chmod +x $APP_HOME/scripts/unix/*.sh
export APP_HOME ANT_HOME PATH
When I try to execute ant command I get an error message as:
-bash: ant: command not found
The echo $ANT_HOME is printing my ant home location the PATH is printed properly too.
After execting setAppPath.sh file I tried echo $ANT_HOME it gave empty line.
Please help me figuring out this issue.
Edit 1: which ant give no ant
I am using sh setAppPath.sh command to execute the sh file.

When you run your script normally, what happens is that your shell starts a new process, the script runs in that process, and when the script is done the process dies and control returns to your shell.
All modifications that the script did to its environment die with it. The changes have no effect on the parent shell. Same if you're trying to run cd in a script and expecting the parent shell to move.
To run your script in the context of your shell and not in a subprocess, use the source or . commands:
source setAppPath.sh
. setAppPath.sh

Related

Java not running in crontab

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.

Java runs OK from command line, but gives "Could not find or load main class" from shell script

The command below runs fine when I execute it on RedHat 6 from command line:
java -cp /my/dir/Dep1.jar:/my/dir/Dep2.jar:/my/dir/MainJar.jar one.of.my.classes.Class1 <args> > log.txt
However, when I execute the following script test1.sh (which has 755 permissions):
#!/bin/sh
export JEXE="java"
export JBDIR="/my/dir"
export ARGS="<args>"
export JARS="${JBDIR}/Dep1.jar:${JBDIR}/Dep2.jar:${JBDIR}/MainJar.jar"
export CLASSPATH=""
for f in `ls ${JBDIR}/*.jar`
do
export CLASSPATH=$CLASSPATH:$f
done
cd "${JBDIR}"
"${JEXE}" -cp "${JARS}" one.of.my.classes.Class1 "${ARGS}" > log.txt
as ./test1.sh from command line, I get Could not find or load main class. What am I doing wrong?
UPDATE:
Sorry for bad editing of the original question. Corrected.
echo produces java -cp /my/dir/Dep1.jar:/my/dir/Dep2.jar:/my/dir/MainJar.jar one.of.my.classes.Class1 <args>
I added building CLASSPATH trying to get the script to work. Removing it has no effect,i.e. I get the same exact error.
Is it possible that the JAR is build somehow incorrectly? I generate it by running an ant build from Eclipse.
The script worked after I added
export PATH=$PATH:<path to java directory>

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.

Script Not Running

I have a script file that I would like to run whenever my computer starts up. What the script file does is run a .jar file that I have on my desktop.
I first created a .jar file called Hello.jar that is located on my desktop. After that I created a script file (.sh) called Script.sh that has the following contents in it.
cd Desktop;java -jar Hello.jar;
Then I followed this answer to run the file on startup. So as it says I first setup a .desktop file by running this command in the terminal.
sudo cd Desktop
sudo mv Script.sh /usr/bin
Then I did
sudo cd /usr/share/applications
sudo gedit file.desktop &
Then I wrote the following information in gedit.
[Desktop Entry]
Name=Hello.sh
Exec=/usr/bin/file.sh
Type=Application
Terminal=false
And lastly I created a copy of it in this location.
/etc/xdg/autostart/
I then restarted my computer but nothing happened.
sudo cd doesn't do anything! The cd command only takes effect within the current shell - which immediately exits!
Instead you should do sudo bash to launch a root shell. Then run all your commands within that root shell.
Also, I think you forgot to give your script execute permissions. You can do that by changing mv to install.

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

Categories