quinta-feira, abril 08, 2010

How to configure Grub 2 in Ubuntu 9.10 (Karmic Koala)

I've installed Ubuntu version 9.10 (Karmic Koala) recently, and previously I had another operating system installed in my computer. I tried configure Grub (GRand Unified Bootloader) to execute the dual boot screen, so I noticed that this version of Grub was not the same as the previous versions of Ubuntu.


Ubuntu 9.10 uses Grub 2, and there are some new features to make it easy to configure.
First: You should NOT edit the /boot/grub/grub.cfg file. This file replaces the old /boot/grub/menu.lst Grub legacy file. By the way, it no longer exists.

Actually, there is another configuration file /etc/default/grub that can be edited by a root user (like the old menu.lst). Some of the parameters of this file are:

GRUB_DEFAULT: Sets the default and pre-selected menu entry. Entries may be numeric or saved.

GRUB_DEFAULT: Sets the default menu entry by menu position. As with Grub Legacy, the first "menuentry" in grub.cfg is 0, the second is 1, etc.

GRUB_DEFAULT: Sets the default menu entry by name. Example: "Windows XP Professional (on /dev/sda1)".

GRUB_DEFAULT: Sets the default menu entry with whatever was selected on the last boot. If the menu is displayed during boot, the previously selected option will be highlighted. If no action is taken, this is selection which will be booted at the end of the timeout, or if the menu is hidden.

GRUB_TIMEOUT: No change from Grub Legacy. This is the number of seconds before the default entry is automatically booted.
Setting a value of -1 will display the menu until the user makes a selection (no timeout).

There are another parameters that were ommited in this post.

After you edit /etc/default/grub, run the command update-grub at console as root user, so /boot/grub/grub.cfg will be updated automatically by system.

For further information about Grub 2, read: https://wiki.ubuntu.com/Grub2.

segunda-feira, janeiro 12, 2009

How to build JNI applications using Dev-C++ and Netbeans

JNI is an acronym of Java Native Interface. It is a programming framework that allows your Java code to call and to be called by native applications. So, you can write libraries in other languages, such as Assembly, C/C++, or even C#.

Here, we are going to talk about how to write a DLL in C/C++, using Dev-C++ 4.9.9.2 (avaliable at: http://www.bloodshed.net/devcpp.html). Dev-C++ is not upgraded for a long time, so feel free to use CodeBlocks (the process of building a DLL is almost the same. You can download Codeblocks at: http://www.codeblocks.org). I used Dev-C++ because it seemed nice to me.

Before we start, let's talk about some advantages and disadvantages of JNI:

Using JNI you can...:
* Call Windows API functions.
* Increase execution speed.
* Call API functions of some product which is in C or C++, from Java Client.

Some disadvantages:
* Forget "write once run anywhere" (your code is not portable between different platforms).
* It is difficult to debug runtime error in native code.
* There is a potencial security risk.
* Only applications and signed Applets can invoke the JNI.
* There is no garbage collection at the JNI side (This means that you must dealloc the used memory your code previously alloced).

Now that you know the advantages and risks, let's show an example about how to use JNI with Dev-C++ and Netbeans.

First, configure Dev-C++. Go to Tools -> Compiler Options and create a new compiler set.



Now, go to the Directories tab (as shown bellow) and add the include paths of JDK to C and C++ (be careful here! As I said, you should add to C and then to C++ !!!).

You should add the following directories (you can find them in the JDK folder):
C:\Program Files\Java\jdk1.6.0_07\include
C:\Program Files\Java\jdk1.6.0_07\include\win32

Those are the folders of my computer! They can change according to your JDK version, OS languaage/partition. Be careful!
If you are curious, inside those folders you can find the jni.h file, that you are going to need ahead.

Now that you configured Dev-C++, go to File -> New -> Project (at the top menu), and create a DLL project.


Then, delete the files that came with the project (in the example: dllmain.cpp and dll.h).


Now, create new files in the project (Example.h and Example.cpp).




Here you can see the JNI side code. The header files and functions should be written as shown. The declaration of a function is composed by the following: Java_PackageName_ClassName_methodName (look again to the images above).
To create a DLL, simply Compile your DLL project (in the top Menu, go to: Execute -> Compile).

Finally, create a Java Application at NetBeans (avaliable at: http://www.netbeans.org). At the java project, you should create a package equals to PackageName and a class equals to ClassName.
Copy the DLL into your java project folder.



Now, make a reference to your DLL using System.loadLibrary method. To call a native method, you should create a "signature" method on Java side, using the native keyword. See the code bellow:



Now, compile you class and run it! :-)

Have fun! =]