The problem of project root directory when shell script starts java program under different paths

problem description

Type the java project into tar.gz format, and then extract it on Linux. File directory:

./
    -conf/
    -bin/
        -startup.sh
    -lib/
    -logs/
    -README.md

I execute . / bin/startup.sh start the java program.
but the annoying thing is:

if the path is different when starting the script, the root directory of the project will be different .

  1. when sh bin/startup.sh is executed under the tar root directory, java will use the tar package root directory as the project root directory.

Code new File ("logs/xx.log") , will generate . / logs/xx.log , which is the desired effect.

  1. however, when sh startup.sh is executed under . / bin/ , new FIle ("logs/xx.log") will generate xx.log files under . / bin/logs/ . This is obviously not the desired effect, and it"s annoying.

startup.sh

-sharp!/bin/bash

current_path=`pwd`

case "`uname`" in
    Linux)
        bin_abs_path=$(readlink -f $(dirname $0))
        ;;
    *)
        bin_abs_path=`cd $(dirname $0); pwd`
        ;;
esac

echo ": $bin_abs_path"
-sharpbase=${bin_abs_path}/..
base=$(dirname $(cd `dirname $0`;pwd))
echo "base path: $base"


export LANG=en_US.UTF-8
export BASE=$base

-sharpcan"t run repeatedly
if [ -f $base/bin/addr.pid ] ; then
    echo "found bin/addr.pid , Please run stop.sh first ,then startup.sh" 2>&2
    exit 1
fi

-sharp-sharp set java path
if [ -z "$JAVA" ] ; then
  JAVA=$(which java)
fi



str=`file $JAVA_HOME/bin/java | grep 64-bit`
if [ -n "$str" ]; then
    JAVA_OPTS="-server -Xms1024m -Xmx1536m -Xmn256m -XX:SurvivorRatio=2 -XX:PermSize=96m -XX:MaxPermSize=256m -Xss256k -XX:-UseAdaptiveSizePolicy -XX:MaxTenuringThreshold=15 -XX:+DisableExplicitGC -XX:+UseConcMarkSweepGC -XX:+CMSParallelRemarkEnabled -XX:+UseCMSCompactAtFullCollection -XX:+UseFastAccessorMethods -XX:+UseCMSInitiatingOccupancyOnly -XX:+HeapDumpOnOutOfMemoryError"
else
    JAVA_OPTS="-server -Xms1024m -Xmx1024m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:MaxPermSize=128m "
fi

JAVA_OPTS=" $JAVA_OPTS -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dfile.encoding=UTF-8"

for i in $base/lib/*;
    do CLASSPATH=$i:"$CLASSPATH";
done


-sharp$JAVA $JAVA_OPTS -classpath .:$CLASSPATH com.jfai.addr.StartUp 1>>$base/bin/nohup.out 2>&1 &
$JAVA $JAVA_OPTS -classpath .:$CLASSPATH com.jfai.addr.StartUp 1>$base/bin/nohup.out 2>&1 &

echo $! > $base/bin/addr.pid
echo "Process addr is running..., pid=$!"
cd $current_path

you can cd the directory to the directory you want to go to at the beginning of startup.sh. Don't rely on the caller's current directory. For example, if you want to enter the bin directory, add

at the beginning.
cd `dirname "$0"` 

if you want to be in the next directory, just

cd `dirname "$0"`/.. 
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e50ffd-5a146.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e50ffd-5a146.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?