How to Install Java Application to My Linux System

How to install Java application to my linux system

One neat little trick is that you can append a basic shell script to the start of the jar file which will run it appropriately. See here for the full example but the basics are:

stub.sh

#!/bin/sh
MYSELF=`which "$0" 2>/dev/null`
[ $? -gt 0 -a -f "$0" ] && MYSELF="./$0"
java=java
if test -n "$JAVA_HOME"; then
java="$JAVA_HOME/bin/java"
fi
exec "$java" $java_args -jar $MYSELF "$@"
exit 1

Then do...

cat stub.sh helloworld.jar > hello.run && chmod +x helloworld.run 

And you should be all set! Now you can just call the script-ified jar directly.

./helloworld.run

How to install the JDK on Ubuntu Linux

Referring to Ask Ubuntu question How to set JAVA_HOME for OpenJDK?,

How to install Open JDK (Java developement kit) in Ubuntu (Linux)?

  1. Open Terminal from Application Dash or press Ctrl+Alt+T

  2. Update repository:

    sudo add-apt-repository ppa:openjdk-r/ppa  # only Ubuntu 17.4 and earlier
    sudo apt update
  3. Optional: To search available distributions of openjdk, use the following command:

    apt search openjdk
  4. Install the appropriate version with the following command:

    sudo apt install openjdk-8-jdk
    sudo apt install openjdk-8-source #this is optional, the jdk source code
  5. For JAVA_HOME (Environment Variable) type command as shown below, in "Terminal" using your installation path...

    export JAVA_HOME=/usr/lib/jvm/java-8-openjdk

    (Note: /usr/lib/jvm/java-8-openjdk is symbolically used here just for demostration. You should use your path as per your installation.)

  6. For PATH (Environment Variable) type command as shown below, in Terminal:

    export PATH=$PATH:$JAVA_HOME/bin

  7. To check your installation:

    java -version

Run a Java Application as a Service on Linux

I wrote another simple wrapper here:

#!/bin/sh
SERVICE_NAME=MyService
PATH_TO_JAR=/usr/local/MyProject/MyJar.jar
PID_PATH_NAME=/tmp/MyService-pid
case $1 in
start)
echo "Starting $SERVICE_NAME ..."
if [ ! -f $PID_PATH_NAME ]; then
nohup java -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
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 -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started ..."
else
echo "$SERVICE_NAME is not running ..."
fi
;;
esac

You can follow a full tutorial for init.d here and for systemd (ubuntu 16+) here

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&

Java application on Linux

It depends on what your client code is doing. If it has very minimal requirements, then you can bundle a linux kernel (from any distribution), a bootloader, a JRE and your client code. The JRE and client code should go into a file system, perhaps an initial ram fs so you don't have to worry about disk file systems. If you compile the kernel from source, you can even give a directory containing the initramfs content, so you could build this directly into the kernel. You'd then plug the JRE invocation as the init= argument on the kernel command line. Again, if you compile the kernel from source, you can include parts of the command line there.

The above assumes that you require no userland tools at all. This is an increasingly inappropriate assumption for most modern systems, since userland tools are used to load drivers, firmware, device configurations, and a million other jobs required to get the system up and running. So you might have to supply a minimal userland. Have a look at busybox for ways to accomplish this. Many embedded linux devices choose this route.

If your client should be able to present a graphical user interface, then you need an X server as well. You can compile that from source as well, but with all the different drivers involved here, and all the different dependencies, I'd suggest building on an existing distribution here. debootstrap can e.g. be used to create a minimal Debian or Ubuntu installation, which might be used as a starting point for further customizations. This is the route I've chosen for one of my own projects.

I don't want to use any LINUX existing OS like Ubuntu or any other and I need to compile my Linux Kernel

I see little chance of creating a customized Linux installation (in one of the shapes described above) without using a full-featured linux environment for the development. If you don't feel at home in a multi-boot setup, you can run your linux build environment in a virtualized environment, e.g. using qemu or virtualbox.

how to do java installation in Linux(Ubuntu)

In the link you have provided above there is not mention about version of java. Also, the installation steps are direct.

Anyway to just specify an exact version of a package using apt-get you could do(apache2 installation example)

sudo apt-get install apache2=2.2.20-1ubuntu1

go through this link



Related Topics



Leave a reply



Submit