Showing posts with label DLL. Show all posts
Showing posts with label DLL. Show all posts

December 27, 2011

Java Native Interface (JNI) in Netbeans with C/C++ and Java Hello World Sample Source Code

Hi all,

Today I configured the Netbeans IDE to work with Java Native Interface (JNI).

Pre-Requisites:
  1. Java Development Kit (JDK)
  2. Cygwin (or) MinGW & MSYS
  3. NetBeans IDE with Java and C/C++ Bundle

Installation:
  • Install JDK
  • Install Cygwin (or) MinGW&MSYS
  • Note: While installing please choose the destination directory name without spaces
  • I have installed NetBeans IDE All Version, because I thought, I will need all the technologies. That is different approach.

Configuration:
  • Set the environment variables like JAVA_HOME and PATH with Java Installation Directory and JDK bin, Cygwin/MinGW&MSYS bin directories respectively
  • Cygwin/MinGW integration with NetBeans
  • Configure the C/C++ with Tools MinGW bin directory, which automatically fills the corresponding compilers

Source Code:

  • Create a sample java application with the following detail

package javaapp1.JavaApp1;

public class JavaApp1
{
public static void main(String [] args)
{
  new JavaApp1.nativePrint();
}

private native void nativePrint();
}

  • Clean and Build the java project
  • In Command Prompt, Navigate to Netbeans Project Directory
    • cmd> cd Netbeans/ProjectDir
  • Create the Java header file
    • cmd> javah -o JavaApp1.h -classpath JavaApp1\build\classes javaapp1.JavaApp1

  • Create a C/C++ Dynamic Library Project in Netbeans (In this example, it is CppDll1)
  • Change some of the project properties
  • Make sure that, you have downloaded and configured the proper Cygwin/MinGW with exact Architecture 32bit/64bit
  •  Make sure that you are using proper gcc for 32bit/64bit dll creation
  • Linker must be of same architecture, 32bit/64bit


  • Include the Java header file, which we have created in Java Project.
    • Right click on the "Source Files"-> Select "Add an Existing Item"
    • Select the created header file from Project Home Directory
    • (OR)
    • Manually copy the created header file from Project home directory to CppDll directory
  • Create a C Source file in "Source Files" in CppDll1 project
 #include "JavaApp1.h"

JNIEXPORT void JNICALL Java_javaapp1_JavaApp1_nativePrint(JNIEnv *env, jobject obj)
{
    printf("\nHello World from C\n");  
}
  • Clean and Build the C code. I have faced the following issue.
    • Issues Faced:
      • I am using Windows 7 64-bit and installed Jdk 64-bit. But initially I have installed MinGW 32 bit and it is generating only 32 bit DLL which is not suitable to run the Java application in NetBeans.
    • Resolution:
      • Downloaded MinGW 64 bit and configured in the NetBeans C/C++ Options
  • Copy the .dll file path
  • In Java project, open the main class and add the following content
package javaapp1.JavaApp1;

public class JavaApp1
{
static
{
  System.load("dll file path");
}
public static void main(String [] args)
{
  new JavaApp1.nativePrint();
}

private native void nativePrint();
}
  • Clean and Build the project
  • Run the Java project to see the final JNI output.

References:
http://java.sun.com/docs/books/jni/html/jniTOC.html
http://java.sun.com/developer/onlineTraining/Programming/JDCBook/jni.html
http://www.cygwin.com/
http://sourceforge.net/projects/mingw-w64/
http://netbeans.org/community/releases/71/install.html

Please send your feedback to psrdotcom@gmail.com

December 24, 2011

Customize Command Prompt in Windows 7 with your own Icon, Starting Directory and more settings ...

Hi all,

Are you getting bored with the same command prompt with black colored background and other settings?

You can customize your own command prompt and you can start from the preferred location (Directory) path, choose your own icon and lot more.

Go ahead by following the below steps ..


Creating the shortcut icon on Destop
  1. Right click on the desktop, select "New" -> "shortcut"
  2. Browse for the command prompt executable file "cmd.exe"
  3. If you have installed your Operating System in C drive then the path might be similar to this
  4. C:\Windows\System32\cmd.exe
  5. Click on "Next"
  6. Type the name for this shortcut, something like "MyCmdPrompt"
  7. Click on "Finish"
  8. Now you can see your command prompt named of your own choice
  9. Now, you can change some properties of your command prompt

Changing the properties of the custom command prompt

  • Right click on the newly created shortcut from the Desktop
  • Select "Shortcut" tab and change the following settings
  1. Change the starting directory: Specify your own choice of directory path in the "Start in" box.
  2. Default Start in path: C:\Windows\System32
  3. Change the icon of your own choice: Click on "Change Icon" and Click on "Browse" button and Select the "C:\Windows\System32\imageres.dll" to list all the windows installed icons.
  4. Select an icon of your own choice and Click on "Ok"
  5. Run as Administrator by default: Click "Advanced" button, Select "Run as administrator" and Click "Ok"
  • Select "Options" tab and change the following settings
    1. Check "Quick Edit mode" to copy and paste easily
  • Select "Font" tab and change the following settings
    1. select your desired "Font" and "Size" settings
  • Select "Layout" tab and change the following settings
  1. select your desired "Window Size"
  2. Note: To see a bigger screen, increase the "Width and Height" attributes of the "Window Size"
  3. You can preview, your settings in the left side preview
  • Select "Colors" tab, You might be bored by seeing the same black and white combination.
  1. Choose your desired colors for Background, Foreground colors for both the "Screen" and "Pop-Up screen"
  • Click on "Apply" and then "Ok" to see your own command prompt.

Hope this post is useful to you customize your own Command Prompt in Windows 7.

For further queries, mail to psrdotcom@gmail.com

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com