Thursday, November 26, 2009

How to pack native libraries (JNI) into a jar.

Usually the native libraries are passed to the VM with the property java.library.path. This is not pratical when we want to provide a single jar, and we cannot pass an URL to System#load(String).
Therefore we adopt the strategy to copy the native library contained in the jar (or simply classpath) in the system temporary folder and than we
pass this filename to System#load(String).

As for the standard JDK method, this one should be invoked at classloading:

static {
  try {
   loadLibrary("my.package", "HelloWorld");
  } catch (IOException e) {
   // handle this exception
  }
 }

This is our implementation:
/**
 * Load native libraries packed in a jar.
 *
 * @param pkg
 *            The package where the shared library (for example .so or .ddl)
 *            is.
 * @param libname
 *            The system indipendent name (for example HelloWorld for
 *            libHelloWorld.so under linux or HelloWorld.dll under windows).
 * @throws IOException
 */
static public void loadLibrary(String pkg, String libname) throws IOException {
 String syslibname = System.mapLibraryName(libname);
  String tmpdir = System.getProperty("java.io.tmpdir");
 File file = new File(tmpdir + File.separator + syslibname);
 if (!file.exists()) {
  String resourcename = "/" + pkg.replace('.', '/') + "/" + syslibname;
  writeToFile(GtkVersion.class.getResourceAsStream(resourcename), file);
 }
  System.load(file.getAbsolutePath());
}
 static private void writeToFile(InputStream stream, File file) throws IOException {
 OutputStream os = null;
 try {
  os = new FileOutputStream(file);
  byte buffer[] = new byte[4096];
  int len = 0;
  while ((len = stream.read(buffer)) > 0) {
   os.write(buffer, 0, len);
  }
 } finally {
  if (os != null) {
   os.close();
  }
  stream.close();
 }
}

Monday, May 18, 2009

Generating Java Beans from XSD Schema

As you probably know, JAXB (Java Architecture for XML Binding) allows to map (marshal/unmarshal) Java Beans to XML representations.
You can also do the opposite: generating Java Beans from XSD Schema.
To do that, there the xjc utility in $JAVA_HOME/bin.
For example:
$ wget http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
$ xjc -p eu.kostia.persistence persistence_1_0.xsd

will generate the classes ObjectFactory, Persistence and PersistenceUnitTransactionType in the package eu.kostia.persistence.

Other useful info here.

Wednesday, April 22, 2009

Best Gnome Theme for the GTK Look and Feel

The GTK Look and Feel was a little bit buggy under gnome, but a lot of improvements were done with the last JDK 1.6 Update 13. Anyway I had still some problems with comboboxes and tabbedpanes.
I used the default Fedora Gnome theme. Now I've switched to Glossy Theme and everything looks perfectly in my java applications.
See with your eyes: the second example uses the Glossy Theme and the combo are rendered correctly.





Also the universal db client SQuirrel looks very good!:


Tuesday, January 20, 2009

Video Plugins for Firefox under Fedora 10

The most important video player/library under linux are Gstreamer, Xine, Mplayer and VLC.
The first two are plug-in based, while the last two are "all-in-one" solutions.

Many of the packages that you need to see the most common proprietary video formats aren't in the standard Fedora repository, therefore you have to add RPMFusion

Of course to see videos in Firefox you need a plugin. To see the videos at http://tv.repubblica.it I've tried all these plugins:
  1. xine-plugin: It couldn't recognise the format and came the message "Format x-ms-asf unknown"
  2. mplayerplug-in: It isn't in RPMFusion but in ATrpms. These two repositories aren't compatible and it's better to not enable them together. Personally I prefer RPMFusion because there the packages are more stable. Anyway with this plugin I could see the video stream but in other sites firefox crashed. It was quite unstable.
  3. mozilla-vlc: I coudn't see anything, maybe I hadn't VLC complete installed.
  4. totem-mozplugin: Totem is the standard player for Gnome. At the beginning I coudn't see anything, but after installing mit yum gstreamer-plugins* and gstreamer-ffmpeg it worked perfectly. It appears also to be very stable and it's the plugin that I'm now currently using.
With the totem-mozplugin I tried then to see a video on http://www.rai.tv: after a short advertising clip the reproduction was interrupted. I clicked with the mouse right button on the video frame and copied the video source URL. I was able to reproduce it with mplayer but not with totem.

Friday, January 16, 2009

Change Resolution in Fedora 10 to undetected value

I'recently bought a Thinkpad T500. It came with Vista but I've immediately installed the last version of Fedora.
After the installation, that didn't present any particular problem, I wanted to switch the resolution to 1280x800.
You can set the resolution directly in Gnome, under:
System -> Preferences -> Hardware -> Screen Resolution.
Unfortunately my desired resolution wasn't automatically detected, therefore I should to set it manually.

To do this follows this steps:
$ gtf 1280 800 60

# 1280x800 @ 60.00 Hz (GTF) hsync: 49.68 kHz; pclk: 83.46 MHz
Modeline "1280x800_60.00" 83.46 1280 1344 1480 1680 800 801 804 828 -HSync +Vsync

$ xrandr --newmode "1280x800_60.00" 83.46 1280 1344 1480 1680 800 801 804 828 -HSync +Vsync
$ xrandr --addmode LVDS "1280x800_60.00"
$ xrandr --output LVDS --mode "1280x800_60.00"

Where LVDS is your video output.
To get all outputs of you sistem use:
xrandr --prop

That's it! To persist this setting, add it to the beginning of /etc/gdm/Init/Default.

Don't forget to correctly set also your dpi, going to
System -> Preferences -> Look ans Style -> Look
and then in the tab "Fonts type" click on the button "Details"

To compute the optimal dpi for you system, use this formula:
dpi = sqrt(w^2 + h^2) / d

where:
* w is the screen width resolution in pixels,
* h is the screen height resolution in pixels and
* d is diagonal size in inches.

In my case the value for a 1280x800 resolution on a 15.4 LCD Screen was 98.

Thursday, January 8, 2009

Cisco AnyConnect VPN Client under Fedora

In order to make the "Cisco AnyConnect VPN Client" working under Linux Fedora, you need the following symbolic links
sudo ln -s /lib/libnss3.so /usr/lib/libnss3.so
sudo ln -s /lib/libplc4.so /usr/lib/libplc4.so
sudo ln -s /lib/libnspr4.so /usr/lib/libnspr4.so
sudo ln -s /lib/libsmime3.so /usr/lib/libsmime3.so

Then, for safety, uninstall it with /opt/cisco/vpn/bin/vpn_uninstall.sh und try again to establish a connection.