Skip to content

Instantly share code, notes, and snippets.

@emrahoruc
Last active February 12, 2021 15:53
Show Gist options
  • Save emrahoruc/5b2ed9b44293f7c944f058449b12b70e to your computer and use it in GitHub Desktop.
Save emrahoruc/5b2ed9b44293f7c944f058449b12b70e to your computer and use it in GitHub Desktop.
Scala Notes

CentOS 7

Java
yum install java-1.8.0-openjdk

Setting the JAVA_HOME Variable

sudo cp /etc/profile /etc/profile_backup      #Backup the profile file in order to prevent unintentional mistakes
echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk' | sudo tee -a /etc/profile
echo 'export JRE_HOME=/usr/lib/jvm/jre' | sudo tee -a /etc/profile
source /etc/profile

Scala

cd ~
wget downloads.lightbend.com/scala/2.13.1/scala-2.13.1.rpm
sudo yum install scala-2.13.1.rpm

1- Create a file under /etc/init.d/ with nano or vi and paste the example script below. ex. sudo vi /etc/init.d/mytestserv
2- Modify the SERVICE_NAME, PATH_TO_JAR, and choose a PID_PATH_NAME for the file you are going to use to store your service ID.
3- Write the file and give execution permisions ex. sudo chmod +x /etc/init.d/mytestserv
4- Test that it runs ex. sudo service mytestserv start
5- Test that it stops ex. sudo service mytestserv stop
6- Test that it restarts ex. sudo service mytestserv restart

#!/bin/sh
SERVICE_NAME=MyApp
PATH_TO_JAR=/home/path/MyApp.jar
PATH_TO_LOG=/home/path/MyApp.log
PATH_TO_CONF=/home/path/my-app.conf
PID_PATH_NAME=/tmp/MyApp-pid
case $1 in
    start)
        echo "Starting $SERVICE_NAME ..."
        if [ ! -f $PID_PATH_NAME ]; then
            nohup  java -cp . -XX:+UseConcMarkSweepGC -Dconfig.file=$PATH_TO_CONF -jar $PATH_TO_JAR -Xmx2000M >> $PATH_TO_LOG 2>&1&
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is already running ..."
        fi
    ;;
    stop)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stoping ..."
            kill $PID;
            echo "$SERVICE_NAME stopped ..."
            rm $PID_PATH_NAME
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
    restart)
        if [ -f $PID_PATH_NAME ]; then
            PID=$(cat $PID_PATH_NAME);
            echo "$SERVICE_NAME stopping ...";
            kill $PID;
            echo "$SERVICE_NAME stopped ...";
            rm $PID_PATH_NAME
            echo "$SERVICE_NAME starting ..."
            nohup  java -cp . -XX:+UseConcMarkSweepGC -Dconfig.file=$PATH_TO_CONF -jar $PATH_TO_JAR -Xmx2000M >> $PATH_TO_LOG 2>&1&
                        echo $! > $PID_PATH_NAME
            echo "$SERVICE_NAME started ..."
        else
            echo "$SERVICE_NAME is not running ..."
        fi
    ;;
esac

Modify if you need logs If you need the output log replace the 2

nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &

lines for:

nohup java -jar $PATH_TO_JAR >> myService.out 2>&1&

Source: http://www.jcgonzalez.com/linux-java-service-wrapper-example

Create JAR (Intellij) File > Project Structure > Project Settings > Artifacts > Add(+) > JAR > From modules with dependencies
Create JAR from Modules
Module: All Modules
Main Class: Browse and select main class
JAR files from libraries: copy to the output directory and link via manifest
OK then
Build > Build Artifacts... > X:jar > Build/Rebuild etc.
Files will be in ..\out\artifacts\X_jar

Run
Copy all files from \out\artifacts\X_jar to server.

java -cp . -jar X.jar

Run as a Service
Go to > Linux Java Service Wrapper Example.md

Convert cert to jks certificate
/usr/lib/jvm/jre-1.8.0-openjdk/bin/keytool -import -trustcacerts -keystore /usr/lib/jvm/jre-1.8.0/lib/security/cacerts -storepass changeit -alias cert.crt -import -file cert.crt
or
"C:\Program Files\Java\jdk1.8.0_251\bin\keytool.exe" -import -trustcacerts -keystore "C:\Program Files\Java\jdk1.8.0_251\jre\lib\security\cacerts" -storepass changeit -alias cert.crt -import -file cert.crt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment