JNLP Example

All

Continuing the JNLP part

Let’s create an HelloWorld in jnlp, i.e. an `file.jnlp` that when opened, shows Hello world:

Creating a java swing/awt class:

//Based totally in http://www.mkyong.com :D I hope he allows me
public class TestJnlp {
  static BasicService basicService = null;
  public static void main(String args[]) {
    JFrame frame = new JFrame("Example JNLP");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JLabel label = new JLabel();
    Container content = frame.getContentPane();
    content.add(label, BorderLayout.CENTER);
    String message = "Yo Gabriel";

    label.setText(message);

    try {
      basicService = (BasicService)
        ServiceManager.lookup("javax.jnlp.BasicService");
    } catch (UnavailableServiceException e) {
      System.err.println("Lookup failed: " + e);
    }

    JButton button = new JButton("Button");

    ActionListener listener = new ActionListener() {
      public void actionPerformed(ActionEvent actionEvent) {
        try {
          URL url = new URL(actionEvent.getActionCommand());
          basicService.showDocument(url);
        } catch (MalformedURLException ignored) {
        }
      }
    };

    button.addActionListener(listener);

    content.add(button, BorderLayout.SOUTH);
    frame.pack();
    frame.show();
  }
}

To compile, don’t forget to add `jnlp.jar` in the classpath,

:jnlp.jar:.:

Create a jar with the file: jar -cf TestJnlp.jar TestJnlp.* And sign the jar with your keystore:

[fdemeloj@fdemeloj JNLP]$ jarsigner -keystore eap7console.jks TestJnlp.jar alias

Don’t forget to consult the JAVAWS syntax.

Results

Just do javaws Test.jnlp:

Troubleshooting

For troubleshooting, I would suggest Developer Guide JavaWS

The issues you can find:

1. Wrong location

net.sourceforge.jnlp.LaunchException: Fatal: Read Error: Could not read or parse the JNLP file. You can try to download this file manually and send it as bug report to IcedTea-Web team.
	at net.sourceforge.jnlp.Launcher.fromUrl(Launcher.java:487)
	at net.sourceforge.jnlp.Launcher.launch(Launcher.java:287)

2. JAR not signed

Caused by: net.sourceforge.jnlp.LaunchException: Fatal: Application Error: Cannot grant permissions to unsigned jars. Application requested security permissions, but jars are not signed.
	at net.sourceforge.jnlp.runtime.JNLPClassLoader$SecurityDelegateImpl.getClassLoaderSecurity(JNLPClassLoader.java:2481)
	at net.sourceforge.jnlp.runtime.JNLPClassLoader.setSecurity(JNLPClassLoader.java:385)

3. Main-class

Caused by: net.sourceforge.jnlp.LaunchException: Fatal: Initialization Error: Unknown Main-Class. Could not determine the main class for this application.
	at net.sourceforge.jnlp.runtime.JNLPClassLoader.initializeResources(JNLPClassLoader.java:774)
	at net.sourceforge.jnlp.runtime.JNLPClassLoader.<init>(JNLPClassLoader.java:338)
	at net.sourceforge.jnlp.runtime.JNLPClassLoader.createInstance(JNLPClassLoader.java:421)

Don’t try to execute as jar, this will return to you:

[fdemeloj@fdemeloj test_jnlp]$ java -jar TestJnlp.jar 
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: javax/jnlp/UnavailableServiceException
	at java.lang.Class.getDeclaredMethods0(Native Method)
	at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
	at java.lang.Class.privateGetMethodRecursive(Class.java:3048)

At the end of the day, javaws -help is a friend

To debug:

export JAVAWS_VM_ARGS="-verbose -J-Xdebug -J-Xnoagent -J-Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y -Xnofork"

javaws Test.jnlp $JAVAWS_VM_ARGS #this starts Iced Tea Web


But this is for the third part of this series …

SCTP Protocol

All

Intro

Stream Control Transmission Protocol, or SCTP, this is very well explained in Video explaining, by Russell DeLong.

Basically, is a protocol sending streams of data between two points, with a connection previously done, and is also called TCPing.

One of the usages of SCTP, is VoIP particularly to support the telephone system’s Signaling System 7 – SS7, together with voice . SCTP also is intended to make it easier to manage connections over a wireless network and to manage the transmission of multimedia data.

SCTP is a standard protocol (RFC 2960) developed by the Internet Engineering Task Force (IETF).

Similarities with TCP:

1. SCTP manages “reliable transport” (ensuring the complete arrival of data units that are sent over the network) over the Internet’s basically connectionless Internet Protocol (IP), the protocol responsible for moving the data but not for managing whether all the data arrives.

Differences with TCP

2. SCTP ensures the complete concurrent transmission of several streams of data (in units called messages) between connected end points. SCTP also supports multihoming, which means that a connected end point can have alternate IP addresses associated with it in order to route around network failure or changing conditions.

In Java

In Java we have the `com.sun.nio.sctp` that implements SCTP.

import java.net.*;
import java.util.*;
import java.nio.*;
import com.sun.nio.sctp.*; <---- sctp

public class SctpExample
{
	public static void main(String[] args) throws Exception {
         //Server Channel:
		com.sun.nio.sctp.SctpServerChannel sc = com.sun.nio.sctp.SctpServerChannel.open(); // Open the channel
         
		com.sun.nio.sctp.SctpChannel rc = null;
		InetSocketAddress localAddr = new InetSocketAddress (InetAddress.getByName (args[0]), Integer.parseInt (args[1]));
         // Server Channel Open   
		sc.open(); //Open
		sc.bind(localAddr); //Bind the local address
		
		sc.close(); //Close
	}
}

JNI in Java AND Testing JNI with a few GCs

All

Intro

Although I did some stuff with JNI in Java during my time in schooling, I never wrote it about it here. Nor gave examples.

JNI

Java Native Interface is used basically for C/C++ interfaces, in my example, I was doing the processing of some data in Java but the data was generated (compiled, traced, generated) in C/C++ (where I would collect the data as well). (Btw, it is just me or this new way to write in wordpress with blocks sucks, like really)

That being said, the quickest example with JNI is by doing in a JAVA class

public class JNIExample{  //
   static {
      System.loadLibrary("hello");
   }
 
   private native void sayHello();
 
   public static void main(String[] args) {
      new HelloJNI().sayHello(); 
   }
}

After creating the file, we compile it:

fdemeloj@fdemeloj jni]$ javac JNIExample.java 

Then create the header with javah, and not javac -h as used to be back in Poly times. Write a HelloWorld.c:

#include <jni.h> //import jni header
#include <stdio.h> //Import the stdio
#include "HelloWorld.h" //Insert the header
  
JNIEXPORT void JNICALL 
Java_HelloWorld_print(JNIEnv *env, jobject obj)
{
     printf("Hello World!\n");
     return;
}

Write a Make file to make the compilation process easy for our Shared library

all:
	@echo "Generating Shared Library"
	cc -I. -I${JAVA_HOME}/include -I${JAVA_HOME}/include/linux -fPIC -shared -o libHelloWorld.so HelloWorld.c HelloWorld.h

clean:
	@echo "Cleaning the generated SH files"
	rm *.so

To run the code, just run java (no need for LD_LIBRARY_PATH if you are using -Djava.library.path=.)

export LD_LIBRARY_PATH=. 
[fdemeloj@fdemeloj jni]$ java HelloWorld 
Hello World!

The cool thing is, IT is a shared library totally independent from the JAVA implementation (tcharam, that’s JNI), so then if we want to change the library to this:

 Java_HelloWorld_print(JNIEnv *env, jobject obj)
 {
     printf("After change!\n");
     return;
 }

Just recompile the shared library with Make clean;Make. and It’s all good:

[fdemeloj@fdemeloj jni]$ java HelloWorld 
After change!

Next thing is to test the GC collections with JNI, from this link. From the same process, we run Makefile, that takes the java and compile it.

#include <jni.h>
#include <CriticalGC.h>

static jbyte* sink;

JNIEXPORT void JNICALL Java_CriticalGC_acquire(JNIEnv* env, jclass klass, jintArray arr) {
   sink = (*env)->GetPrimitiveArrayCritical(env, arr, 0);
}

JNIEXPORT void JNICALL Java_CriticalGC_release(JNIEnv* env, jclass klass, jintArray arr) {
   (*env)->ReleasePrimitiveArrayCritical(env, arr, sink, 0);
}

Running it with Shenandoah we see the difference and how it is so fast:

[fdemeloj@fdemeloj critical-gc]$ make run-shenandoah 
time java -Djava.library.path=. -Xms4g -Xmx4g -verbose:gc -XX:+UseShenandoahGC CriticalGC
Acquired
Releasing
Acquired
Releasing
Acquired
Releasing
Acquired
Releasing
Acquired
[Pause Init Mark, 0.533 ms]
[Concurrent marking 2880M->2988M(4096M), 216.052 ms]
[Pause Final Mark 2988M->920M(4096M), 2.220 ms]
[Concurrent reset bitmaps 922M->926M(4096M), 0.946 ms]
Releasing
Acquired
Releasing
Acquired
^CCancelling concurrent GC: Stopping VM
Releasing
Acquired

I mean, comparing with the other GC’s approaches this Shenandoah doesn’t even stop.

REFERENCE:

Following this reference