Tuesday, July 18, 2017

JAVA Web Start (JWS) JNLP Example

JWS is introduced to use client side application processing through web. Clients are downloading JNLP application and execute the application to process data.


  1. Create JAR file that you want to execute it in client side (TestJNLP.jar)
  2. Sign the file using your certificate
    1. keytool -genkey -keystore testKeys -alias test
    2. jarsigner -keystore testKeys TestJnlp.jar test
  3. Create JNLP file (Test.jnlp)
  4. Deploy JNLP file and signed JAR file in web server
  5. Download JNLP file and execute application (http://localhost:8080/Test.jnlp)

Sample JNLP file

<?xml version="1.0" encoding="utf-8"?><jnlp spec="1.0+" codebase="http://localhost:8080/" href="Test.jnlp">
<information>
<title>Jnlp Testing</title>
<vendor>Testing</vendor>
<homepage href="http://localhost:8080/" />
<description>Testing Testing</description>
</information>
<security>
<all-permissions/>
</security>
<resources>
<j2se version="1.6+" />
<jar href="TestJnlp.jar" />
</resources>
<application-desc main-class="com.test.TestJnlp" />
</jnlp>

For Windows OS, change OS type in resource XML tag as follows.

<resources os="Windows">

For native library access, you have to create DLL into JAR file and then signed the JAR file using certificate and deploy into web server. Then, you have to change JNLP file as follows.

<resources  os="Windows">       
          <j2se  version="1.6+" />    
          <jar  href="TestJnlp.jar" />
           <nativelib  href="DLLtoJAR.jar" />
  </resources>   

Create DLL into a JAR file using single command

jar cvf DLLtoJAR.jar testDLL.dll

Resource
https://dzone.com/articles/java-web-start-jnlp-hello

Tuesday, May 9, 2017

MAVEN build plugins

Following plugins can be used to build jar file with sources embedded.

<build>
<!-- adding your sources into jar file -->
<resources>
<resource>
<directory>${basedir}/src/main/java/</directory>
</resource>
</resources>


<plugins>

<!-- copying your resource folder into build directory -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<id>copy-resources</id>
<!-- here the phase you need -->
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/resources</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>

<!-- copying your library folder into build directory -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>

<!-- setting your main runner class -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>./lib/</classpathPrefix>
<mainClass>com.mobitel.dataplan.run.Runner</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>

Wednesday, March 1, 2017

Object to JSON string mapper in JAVA

Following code can be used to convert JAVA objects to JSON string. You have to use jackson JAVA library for objects to JSON conversion.

RequestMessage requestMessage = new RequestMessage();
        RequestEntity requestEntity = new RequestEntity();
        requestEntity.setAccountNo("1234");
        requestEntity.setBillNo("9876");
        requestEntity.setType("S");
        requestMessage.setRequestEntity(requestEntity);

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(requestMessage)); 

Converted JSONstring will like this.

{"requestEntity":{"billNo":"9876","accountNo":"1234","type":"S"}}


Monday, February 13, 2017

Install local jars in maven repository

Following command can be used to install local or internal jar files in maven repository.

mvn install:install-file -Dfile=d:/libs/test.jar -DgroupId=com.company.test -DartifactId=test -Dversion=1.0 -Dpackaging=jar

After installing jar files in maven repository, you can use the jar files in maven project as a dependency in pom.xml.

<dependencies>
<dependency>
<groupId>com.company.test</groupId>
<artifactId>test</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

Sunday, February 5, 2017

JBoss EAP6.4 log4j deadlock error

JBoss6.4 is using log4j 'per deployment logging' function. That means, JBoss application server itself adding log4j into application. When using multiple log4j implementations, log4j deadlock scenario may be caused. To avoid that, it can be added 'jboss-deployment-structure.xml' file into JBoss application WEB-INF.

Error message
------------------
"http-/173.12.241.10:8280-4" #109 daemon prio=5 os_prio=0 tid=0x00007fa3e4163000 nid=0x636a waiting for monitor entry [0x00007fa3ae4e7000]
     java.lang.Thread.State: BLOCKED (on object monitor)
     at java.io.PrintStream.flush(PrintStream.java:335)

Solution
------------
jboss-deployment-structure.xml
<jboss-deployment-structure>
<deployment>
<!-- exclude the logging subsystem to disable deployment unit processors from running -->
<exclude-subsystems>
<subsystem name="logging" />
</exclude-subsystems>
</deployment>
</jboss-deployment-structure>


Tuesday, January 3, 2017

Cross domain security error dwr

You can avoid security checking in client side in dwr by adding following code into web.xml file

<servlet>
<servlet-name>dwr-invoker</servlet-name>
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        <init-param>
           <param-name>debug</param-name>
           <param-value>false</param-value>
        </init-param>
        <init-param>
           <param-name>accessLogLevel</param-name>
          <param-value>EXCEPTION</param-value>
        </init-param>
        <init-param>
          <param-name>crossDomainSessionSecurity</param-name>
          <param-value>false</param-value>
        </init-param>
</servlet>