Showing posts with label executable. Show all posts
Showing posts with label executable. Show all posts

Thursday, 29 June 2017

Create an executable JAR file using eclipse

Create an executable JAR file using eclipse



  1. Right click on your project
  2. Select export from the context menu


3. Select the Runnable JAR file
4.Provide the destination path and launch configuration as mail class of the programme


{ Read More }


Thursday, 1 June 2017

Creating a linux service for an executable JAR

Creating a linux service for an executable JAR



We need an executable JAR to run as a service on ubuntu linux.
For this we used Java Service Wrapper.

Here a simple example of the use of Java Service Wrapper.

I have build a simple java executable jar with code below.
package executable;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {
 private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
 private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
 private void messageLoop(String msg){
     while(true) {
      Date nu = new Date();
      String strDate = sdf.format(nu);
         LOGGER.info(strDate + " : " + msg);
         try 
            { Thread.sleep(10000); } 
         catch (InterruptedException ex) 
            { return; }
     }
 } 
    public static void main(String[] args) {
  LOGGER.info("Start main" );
  Main mainKlasse = new Main();
  mainKlasse.messageLoop("Hier ben ik ;o)");
  LOGGER.info("Einde main" );      
    }
}

Create an user on linux for the service.
useradd jarserviceuser
mkdir /home/jarserviceuser
chown jarserviceuser.jarserviceuser /home/jarserviceuser
vim /etc/passwd   (jarserviceuser:x:1004:1004::/home/jarserviceuser:/bin/bash)

passwd jarserviceuser  ( and an appropiate password )


Directories for the new executable-jar application are created, see statemenst below.
mkdir /opt/jarapplication
mkdir /opt/jarapplication/lib
mkdir /opt/jarapplication/bin
mkdir /opt/jarapplication/conf
mkdir /opt/jarapplication/logs
mkdir /opt/jarapplication/tmp
Copy the executable jar to the application directory.
mv executablejar.jar /opt/jarapplication/lib/executablejar.jar

For configuring an executable jar as a service, the Java Service Wrapper is used.
( http://wrapper.tanukisoftware.com/doc/english/download.jsp )

Below the statemens used for configuring the service wrapper.
cd /opt/jarapplication
wget http://wrapper.tanukisoftware.com/download/3.5.26/wrapper-linux-x86-64-3.5.26.tar.gz
gunzip wrapper-linux-x86-64-3.5.26.tar.gz
tar -xvf wrapper-linux-x86-64-3.5.26.tar
cp ./wrapper-linux-x86-64-3.5.26/bin/testwrapper ./bin/
cp ./wrapper-linux-x86-64-3.5.26/bin/wrapper ./bin/
cp ./wrapper-linux-x86-64-3.5.26/lib/* ./lib/

Create a config file and edit this with the right information.
cp ./wrapper-linux-x86-64-3.5.26/src/conf/wrapper.conf.in /opt/jarapplication/conf/wrapper.conf

vim /opt/jarapplication/conf/wrapper.conf

#  needed starting from 1
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=../lib/executablejar.jar

# Java Additional Parameters
#wrapper.java.additional.1=

wrapper.app.parameter.1=executable.Main

# Application parameters.  Add parameters as needed starting from 1
wrapper.logfile=/var/log/jarapplication/jarapplication.log

# Title to use when running as a console

wrapper.console.title=jarapplication

Create a startup script in etc/init.d
cp ./wrapper-linux-x86-64-3.5.26/src/bin/sh.script.in /etc/init.d/jarapplication
vim /etc/init.d/jarapplication

APP_NAME=jarapplication
APP_LONG_NAME="test executable jar"
WRAPPER_CMD="/opt/jarapplication/bin/wrapper"
WRAPPER_CONF="/opt/jarapplication/conf/wrapper.conf"
PIDDIR="/opt/jarapplication/bin"
RUN_AS_USER=jarserviceuser
chmod 0755 /etc/init.d/jarapplication

get the initlevels right with sysv-rc-conf

Make sure the ownerships, rights and folders are correct.
chmod 0644 /opt/jarapplication/lib/executablejar.jar
chown -R jarserviceuser.jarserviceuser /opt/jarapplication/
mkdir /var/log/jarapplication
chown jarserviceuser.jarserviceuser /var/log/jarapplication/
touch /var/log/jarapplication/jarapplication.log
chown jarserviceuser.jarserviceuser /var/log/jarapplication/jarapplication.log

The service is now available with the service command.
service jarapplication status
service jarapplication help
service jarapplication start
service jarapplication stop

Some extra information on creating a service with java service wrapper can be found here (Jenkins example)

For configuring the LOG4J logging, the LOG4J property file has to be configured in the wrapper.conf.
Following line has to be inserted.
wrapper.java.additional.1=-Dlog4j.configuration=file:/opt/jarapplication/conf/log4j.properties

The file /opt/jarapplication/conf/log4j.properties can be created with following lines. (This lets you control the LOG4J output)
log4j.rootLogger = INFO, STDOUT
#log4j.rootLogger = DEBUG, STDOUT
log4j.appender.STDOUT = org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout = org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern = %5p [%t] (%F:%L) - %m%n









{ Read More }


Monday, 17 April 2017

Create a simple executable JAR with Maven in Eclipse

Create a simple executable JAR with Maven in Eclipse


In eclipse create a new project, a new Maven project, select a simple project(skip archetype selection).
Also select a workspace location from the harddrive.
Enter a group-id like, nl.blogspot.janvanoverveld.executableJar
a Artifact id like : executablejar
and a name and description. (my case both executablejar ).

Now we have a simple maven project.

Open the pom.xml and enter text below:

<project xsi_schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>nl.blogspot.janvanoverveld.executableJar</groupId>
  <artifactId>executablejar</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>executablejar</name>
  <description>executablejar</description>
  
<properties>
<slf4j.version>1.7.0</slf4j.version>
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>

<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>

<!-- TEST dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
</plugin>

<!-- below configuration for adding dependencies into jar -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>

<executions>
<execution>
<phase>package</phase>

<goals>
<goal>shade</goal>
</goals>

<configuration>
<!-- below configuration for making jar executable -->
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>executable.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>  
  
</project>

The class that has to be started, is the class described in the mainClass tag above. Create this package and class, below an example:

package executable;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Main {

private static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

private void messageLoop(String msg){
   while(true) {
    Date nu = new Date();
    String strDate = sdf.format(nu);
       LOGGER.info(strDate + " :: " + msg);
       try 
          { Thread.sleep(10000); } 
       catch (InterruptedException ex) 
          { return; }
   }

}

    public static void main(String[] args) {
LOGGER.info("Start main" );
Main mainKlasse = new Main();
mainKlasse.messageLoop("Hier ben ik ;o)");
LOGGER.info("Einde main" );
    }

}

Create a log4j.properties file in src/main/resources, so we will see output when we start our jar.
The log4j.properties should have entries below
log4j.rootLogger = DEBUG, STDOUT, file
log4j.appender.STDOUT = org.apache.log4j.ConsoleAppender
log4j.appender.STDOUT.layout = org.apache.log4j.PatternLayout
log4j.appender.STDOUT.layout.ConversionPattern = %5p [%t] (%F:%L) - %m%n

Now lets try to run the executable jar. From the commandline go to the worspace directory and execute
mvn clean install.
java -jar ./target/





{ Read More }