Java Calling Native Code

JNI vers Executing Processes

In a Java environment you sometimes need to call native code written in C++ or C.  You can do this in two ways, using the Java Native Interface JNI/JNA, or by executing native stand alone applications.

When you use JNI, the native code runs in the same process as the calling Java code.

When you execute a stand alone application, the application runs as a separate process.

The advantages of using JNI are:

  • Its potentially faster since there is no process switch.
  • Error handling is easy. You can use exception handling in the JNI code to return Java exceptions.
  • It’s easy to call JNI code since you call the JNI code just like a normal Java method.
  • You can share memory between the JNI code and the Java code (potentially faster processing).

The advantages to executing system processes are:

  • Crash protection, the code is isolated through a process boundary.  If the process crashes, you can catch it in Java and continue.
  • You can run the code outside the Java environment easily and develop it independent of Java.

The disadvantages of using JNI are:

  • A crash in JNI code will bring down the Java VM.
  • It’s hard to setup, you must create binding code between Java and JNI.  (Java Native Access (JNA) makes this easier.)
  • Hard to run JNI code outside of Java.

The disadvantages of executing processes are:

  • Starting a process is hard, you must write platform dependent code in Java to start the process.
  • It’s is clumsy to call, you must pass command line arguments, environment variables. and a starting directory.
  • Loading of shared libraries is a hard platform dependent problem.
  • Error handling is convoluted, you get one int return value, or you must parse standard error or standard out.
  • You cannot share memory between the Java code and the JNI code.

You may argue that you can protect the JNI code using C++ exception handling but this is not the case.  Below is an example crasher that is not caught.

int crasher()
        int *ptr = 0;
        *ptr = 1234;
    catch (...)
        // never gets here
        printf("caught exception");
    return 0;

Why doesn’t this work? If this was an option, crashing native code could be protected. It seems like an issue with the Java VM.  Here is a note I found under the JNA documentation:

“Set whether native memory accesses are protected from invalid accesses. This should only be set true when testing or debugging, and should not be considered reliable or robust for applications where JNA native calls are occurring on multiple threads. Protected mode will be automatically set if the system property jna.protected has a value of “true” when the JNA library is first loaded.
If not supported by the underlying platform, this setting will have no effect.”

For the project I was working on the crash protection issue overrode all others.  With our limited testing resources and the complexity of our JNI code, we did not want to take the risk of a crashing bug in the native code bring Java down.  So we switched from JNI to native command line applications.

If you have the resources to test and make your native code stable, JNI is the better approach.  Calling system DLLs with JNA is the best approach for calling stable system features not already in the Java API.  Otherwise consider writing your native code as stand alone command line applications and executing the code in a separate process.

Install WordPress on 1and1

I just set up WordPress and I figured my first post would be about installing it on 1and1.

WordPress blogging software is used by 15% of the top million websites, up about 6% this year! WordPress is free open source software and it has an active community of developers. You can select from hundreds of pre-made templates and selecting one for your site only takes a couple of clicks.

1and1 is a popular and cheap web hosting service. A basic plan is only $5.00 a month.

These instructions were tested on a local Ubuntu 10.4 system installing on 1and1 basic plan with Linux hosting. Similar steps apply if you are running a local Windows system. You can use the same steps for 1and1 advanced and business plans, or there are alternate steps, see below. If you have the Windows based package, 1and1 provides a way for you to switch.

Basically to install WordPress you download WordPress to your system, create a database on 1and1, edit one configuration file, then upload all the files to 1and1. Here are the details.

Download WordPress

The latest WordPress comes as a zip file from To download WordPress open a terminal window and type the following:

cd tmp

Now unzip the file. It will create a folder called wordpress inside your tmp folder. Then delete the zip file to save space.


Now rename the wordpress folder to “blog” (or whatever you want) and rename the wp-config-sample.php file to wp-config.php. The name you choose is the name that users will use to get to your blog. If you use “blog” for example, the url will be

mv wordpress blog
cd blog
mv wp-config-sample.php wp-config.php

Find your 1and1 domain name and user name

Your domain name and user name are needed to copy files to 1and1.  To find out what they are follow these steps:

  • In your browser go to
  • Click on Customer Login (near the top right of the page) and enter your credentials.
  • Click on your package.
  • Find your .us domain name. It will be near the top and it looks something like: s123456789.

Note: I could not figure out how to select the domain name for copy and paste. So instead, I selected everything on the page with Ctrl+A, then pasted that into a text editor. There I could select the domain name.

While you are still logged into 1and1 find your user name and set up a password for it.

  • Click on Domains & Web Space.
  • Click on FTP Account.
  • Find your user name. It will look something like: u12345678.
  • Edit the entry and add a password, if not already done.

Note: Don’t add a password if you share the account with someone else and it has already has been set up, because they will no longer be able to ftp. Instead ask the other user for the ftp password.

Note: The password for SSH and the password for FTP are identical. If you change one, it changes the other.  The password to log into your account is different than the SSH and FTP one.

Create WordPress Database

While you are still connected to 1and1 create the database for WordPress. Write down the database information for use when editing wp-config.php later. Here are the steps:

  • Click on MySQL Database Set Up and Configuration.
  • Click on New Database .
  • Write down the database name, host name, user name and database password. They should look similar to:
  1. db123456789 — database name
  2. db123456789.db. — host name
  3. dbo123456789 — user name
  4. mydbpassword – database password

You are done with the 1and1 web interface.

Edit wp-config.php

Edit wp-config.php using your favorite text editor changing the database name, user name, remote host and database password to the names you just wrote down in the last step. Also change the salt values to unique values.

gedit wp-config.php

The lines should look something like this:

define(‘DB_NAME’, ‘db123456789’);
define(‘DB_USER’, ‘dbo123456789’);
define(‘DB_PASSWORD’, ‘mydbpassword’);
define(‘DB_HOST’, ‘db123456789.db.’);

For the salt values change the strings “put your unique phrase here” to something else.

define(‘AUTH_KEY’, ‘12345 zzxcv asdf qwerty zxcv’);
define(‘SECURE_AUTH_KEY’, ‘put 1234 unique phrase 1235’);

Upload Files to 1and1

Now all the files are configured and ready to upload to 1and1.

The 1and1 basic plan supports sftp but not ssh or scp. Using the command line with sftp is too tedious. Instead upload the blog folder to 1and1 using Filezilla. Filezilla is a free GUI based program for ftp. For Windows go to and download the Filezilla client, for Ubuntu, Filezilla can be downloaded and installed with:

 sudo apt-get install filezilla

To run Filezilla type:


Once Filezilla is running enter the connection information to your 1and1 account. Select the menu File > Site Manager and enter the following information:

  • Host (domain name) =
  • Server type = SFTP
  • Logon type = Normal
  • User = u12345678
  • FTP Password = myftppassword
  • Comments = this is my account on 1and1

Then press the Connect button.

While still in Filezilla, transfer the blog folder to your account by following these steps:

  1. In the left local site window select the folder to transfer from your local machine, ~/tmp//blog.
  2. In the right Remote site window select the destination,  /.
  3. Right click the source folder and select upload. The files will be uploaded.

Almost done. Run the Install Script by switching to your browser and entering the address to the install script, something like this:

Write down your WordPress password.

Now WordPress is installed!

Visit your blog at the URL:

Alternate Steps for Advance and Business Packages

The 1and1 advanced and business packages support ssh which makes installing a little easier (but the basic steps work too).

Instead of downloading to your local machine, configuring then uploading, you login to 1and1 using ssh, download directly to your 1and1 account, then configure in place, no need to upload.

To start, open a terminal window and log into your 1and1 account. If you run Windows locally, you need to set up Putty first. Get your user name and host names by logging into 1and1 as noted above under Find your 1and1 domain name and user name.

ssh u12345678@

Download the latest version of WordPress to your 1and1 account and unzip it. This will create the folder called wordpress with all the files inside it. Then delete the unneeded zip file.


Rename the wordpress folder to “blog” (or whatever you want) and rename the wp-config-sample.php file to wp-config.php.

mv wordpress blog
cd blog
mv wp-config-sample.php wp-config.php

Log into your main account and create a database. Follow the same steps as above under Create WordPress Database.

Switch back to the terminal window and edit wp-config.php changing the database name, user name, remote host and database password to the names you just wrote down in the last step.

nano wp-config.php

Run the Install Script by switching to your browser and entering the address to the install script, something like this:

Write down your WordPress password.

Now WordPress is installed!

Visit your blog at the URL: