Wednesday, December 2, 2009

Java Native Interface

Here is an example for Java Native Interface
Java application that calls a C function and executes the operation.
Example
Ping program
Step1:
           Write a java program for ping operation.
import java.util.StringTokenizer;



public class PINGMain {




String ipaddress = null;




boolean defragment = false;




int num = 2;




String str = "www.yahoo.com";




String success ="success";




String[] ipaddr = { "www.yahoo.com", "20", "2000", "5000", "500" };




private String destination;




private String packetSize;




private String bytes;




private String time;




        private String ttl;




protected native String ping(String[] ipaddress);




public PINGMain() {




super();




try {




System.loadLibrary("ping");




} catch (UnsatisfiedLinkError e) {




e.printStackTrace();




}




singlePing();




}




private void singlePing() {




int a = 10;




int b = 20;




String string=null;




while(true) {




string = ping(ipaddr);




//System.out.println(string);




StringTokenizer strToken = new StringTokenizer(string);




int numToken = strToken.countTokens();




            destination =   strToken.nextToken();




            bytes = strToken.nextToken();




            time=       strToken.nextToken();




            ttl = strToken.nextToken();




System.out.println("\n Reply from"+" "+destination+" "+"bytes"+" "+bytes+" "+"Time"+" "+time+" "+"TTL"+" "+ttl);




}




}




private String continuousPing() {




return null;




}




private String disconnect() {




//unLoadPing();




//System.out.println("Unloaded SuccessFully");




return success;




}




private void unLoadPing() {




// unLoad();




}




/**




* @return




*/




private String getStatistics() {




return null;




}




public static void main(String ar[]) {




new PINGMain();




}




}






 



Step 2:





   Using javac to compile the PINGMain source file




After you have defined the PINGMain class, save the source code in a file called PINGMain .java. Then compile the source file using the javac compiler as follows:




javac PINGMain.java
This command will generate a PINGMain.class file in the current directory.


Step 3:




Using javah -jni to generate a C header file (PINGMain.h) containing the function prototype for the native method implementation

Next we will use the javah tool to generate a JNI-style header file that is useful when implementing the native method in C. You can run javah on the PINGMain class as follows:





  • javah -jni PINGMain

    The command shown above generates a file named PINGMain.h.






  • Step 4:





    Writing the C implementation (PINGMain.c) of the native method





    The JNI-style header file generated by javah helps you to write C or C++ implementations for the native method. The function that you write must follow the -prototype specified in the generated header file.












    #pragma comment(lib, "wsock32.lib")


    #include "jni.h"


    #include "com_dtt_traffic_ping_PINGMain.h"


    #include "stdlib.h"


    #include "StdAfx.h"


    #include <windows.h>


    #include <winsock.h>


    #include <stdio.h>


    #include <string.h>





    int packetsize;

    int count;


    char *data;//Ping info


    typedef struct tagIPINFO


    {


            u_char Ttl;                             // Time To Live


            u_char Tos;                             // Type Of Service


            u_char IPFlags;                 // IP flags


            u_char OptSize;                 // Size of options data


            u_char FAR *Options;    // Options data buffer


    }IPINFO, *PIPINFO;





    typedef struct tagICMPECHO

    {


            u_long Source;                  // Source address


            u_long Status;                  // IP status


            u_long RTTime;                  // Round trip time in milliseconds


            u_short DataSize;               // Reply data size


            u_short Reserved;               // Unknown


            void FAR *pData;                // Reply data buffer


            IPINFO  ipInfo;                 // Reply options


    }ICMPECHO, *PICMPECHO;





    // ICMP.DLL Export Function Pointers

    HANDLE (WINAPI *pIcmpCreateFile)(VOID);


    BOOL (WINAPI *pIcmpCloseHandle)(HANDLE);


    DWORD (WINAPI *pIcmpSendEcho)


            (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD);





    //

    //


    char *pingFun(char *dest[])


    {


        int timeout;


        char ping_Resp[2048];


            WSADATA wsaData;                        // WSADATA


            ICMPECHO icmpEcho;                      // ICMP Echo reply buffer


            HANDLE hndlIcmp;                        // LoadLibrary() handle to ICMP.DLL


            HANDLE hndlFile;                        // Handle for IcmpCreateFile()


            LPHOSTENT pHost;                    // Pointer to host entry structure


            struct in_addr iaDest;              // Internet address structure


            DWORD *dwAddress;                       // IP Address


            IPINFO ipInfo;                          // IP Options structure


            int nRet;                                       // General use return code


            DWORD dwRet;                            // DWORD return code


            int x;





           packetsize = atoi(dest[2]);

            count = atoi(dest[5]);


            timeout = atoi(dest[4]);


            // Dynamically load the ICMP.DLL


            hndlIcmp = LoadLibrary("ICMP.DLL");


            if (hndlIcmp == NULL)


            {


                    fprintf(stderr,"nCould not load ICMP.DLLn");


                    return;


            }


            // Retrieve ICMP function pointers


            pIcmpCreateFile = (HANDLE (WINAPI *)(void))


                    GetProcAddress(hndlIcmp,"IcmpCreateFile");


            pIcmpCloseHandle = (BOOL (WINAPI *)(HANDLE))


                    GetProcAddress(hndlIcmp,"IcmpCloseHandle");


            pIcmpSendEcho = (DWORD (WINAPI *)


                    (HANDLE,DWORD,LPVOID,WORD,PIPINFO,LPVOID,DWORD,DWORD))


                    GetProcAddress(hndlIcmp,"IcmpSendEcho");


            // Check all the function pointers


            if (pIcmpCreateFile == NULL             ||


                    pIcmpCloseHandle == NULL        ||


                    pIcmpSendEcho == NULL)


            {


                    fprintf(stderr,"nError getting ICMP proc addressn");


                    FreeLibrary(hndlIcmp);


                    return;


            }





            // Init WinSock

            nRet = WSAStartup(0x0101, &wsaData );


        if (nRet)


        {


            fprintf(stderr,"nWSAStartup() error: %dn", nRet);


            WSACleanup();


                    FreeLibrary(hndlIcmp);


            return;


        }


        // Check WinSock version


        if (0x0101 != wsaData.wVersion)


        {


            fprintf(stderr,"nWinSock version 1.1 not supportedn");


            WSACleanup();


                    FreeLibrary(hndlIcmp);


            return;


        }





            // Lookup destination

        // Use inet_addr() to determine if we're dealing with a name


        // or an address


        iaDest.s_addr = inet_addr(dest[1]);


        if (iaDest.s_addr == INADDR_NONE)


            pHost = gethostbyname(dest[1]);


        else


            pHost = gethostbyaddr((const char *)&iaDest,


                            sizeof(struct in_addr), AF_INET);


            if (pHost == NULL)


            {


                    fprintf(stderr, "n%s not foundn", dest[1]);


            WSACleanup();


                    FreeLibrary(hndlIcmp);


                    return;


            }





            // Tell the user what we're doing

            /*printf("nPinging %s [%s]", pHost->h_name,


                            inet_ntoa((*(LPIN_ADDR)pHost->h_addr_list[0])));*/





            // Copy the IP address

            dwAddress = (DWORD *)(*pHost->h_addr_list);





            // Get an ICMP echo request handle

            hndlFile = pIcmpCreateFile();


           // for (x = 0; x < 4; x++)


                    // Set some reasonable default values


                    ipInfo.Ttl =dest[3];


                    ipInfo.Tos = 0;


                    ipInfo.IPFlags = 0;


                    ipInfo.OptSize = 0;


                    ipInfo.Options = NULL;


                    //icmpEcho.ipInfo.Ttl = 256;


                    // Reqest an ICMP echo


                    dwRet = pIcmpSendEcho(


                            hndlFile,               // Handle from IcmpCreateFile()


                            *dwAddress,             // Destination IP address


                            packetsize,                   // Pointer to buffer to send


                            0,                              // Size of buffer in bytes


                            &ipInfo,                // Request options


                            &icmpEcho,              // Reply buffer


                            sizeof(struct tagICMPECHO),


                            timeout);                  // Time to wait in milliseconds


                    // Print the results


                    iaDest.s_addr = icmpEcho.Source;


                   /* printf("\n Reply from %s  Time=%ldms  TTL=%d",


                                    inet_ntoa(iaDest),


                                    icmpEcho.RTTime,


                                    icmpEcho.ipInfo.Ttl);*/


                    sprintf(ping_Resp,"%s %d %ld %d",    
                    inet_ntoa(iaDest),


                    packetsize,


                    icmpEcho.RTTime,


                    icmpEcho.ipInfo.Ttl);


                    if (icmpEcho.Status)


                    {


                           // printf("nError: icmpEcho.Status=%ld",


                            //        icmpEcho.Status);


                            printf("\n Request timed out");


                            //break;


                    }


            //printf("\n");


            // Close the echo request file handle


            pIcmpCloseHandle(hndlFile);


            FreeLibrary(hndlIcmp);


            WSACleanup();


            return ping_Resp;


    }


    JNIEXPORT jstring JNICALL Java_com_dtt_traffic_ping_PINGMain_ping


       (JNIEnv *env, jclass job, jobjectArray oarr)


    {


       jsize argc = (*env)->GetArrayLength(env, oarr);





       /* Declare a char array for argv */

       int i;


       char const* argv[128];





       for (i = 1; i < 6; i++)

       {


          /* obtain the current object from the object array */


          jobject myObject = (*env)->GetObjectArrayElement(env, oarr, i-1);





          /* Convert the object just obtained into a String */

          const char *str = (*env)->GetStringUTFChars(env,myObject,0);


          /* Build the argv array */


          argv[i] = str;


      }


         data=(char *)malloc(sizeof(char) * 2048);


         sprintf(data,"%s",pingFun(argv),"-10000");


        // printf("Data %s ",data);


          return ((*env)->NewStringUTF(env,data));












    }












    Step5:




            Compiling the C implementation into a native library, creating ping.dll or libPINGMain.so





    On Solaris or Linux, the following command builds a shared library called libPINGMain.so:


    cc -G -I/java/include -I/java/include/solaris

    PINGMain.c -o libPINGMain.so


    On Win32, the following command builds a dynamic link library (DLL) ping.dll using the Microsoft Visual C++ compiler:



  • cl -Ic:\java\include -Ic:\java\include\win32

    -MD -LD PINGMain.c -ping.dll


    Step 6: Running the PINGMain.java



    java PINGMain




        If you get the following error put the dll file in the java path


    java.lang.UnsatisfiedLinkError: no PINGMain in library path

    at java.lang.Runtime.loadLibrary(Runtime.java)


    at java.lang.System.loadLibrary(System.java)


    at PINGMain .main(PINGMain.java)

    Tuesday, December 1, 2009

    Java Oracle Connection

    Step1 : First download the classes12.jar file.
    Step2:  Add the jar file to java library path
    For eclipse –  
    goto Projectmenu--->properties--->Click Java Build Path ---> Libraries Tab—> Add External JARs

    Example Program for JavaWithOracleConnection
    Replace the bold values with your settings.
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;
    public class TestOracleConnection {
    public static void main(String[] args) {
    try {
    Connection con = null;
    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection(
    "jdbc:oracle:thin:@ipaddressoforacleinstalledmachine:1521:databasename", "username",
    "password");
    Statement s = con.createStatement();
    ResultSet rs = s.executeQuery("SELECT * from tablename");
    while (rs.next()) {
    System.out.println("Values------>" + rs.getString("columnname"));
    }
    s.close();
    con.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }

    Friday, November 27, 2009

    Still Blog Does Not Appear In Google Search

    You have a blog with some posts but still your blog not displaying in search results then this post will be useful for you.
    Following steps will be useful for your blog/website appear in Google search engine.
    Step1:
    Verify your blog with Google webmaster tool.
    above post will help you to handle Google webmaster tool.
    if you got any crawl errors,solve it first.
    Step2:
    Get your friends to link to your blog, where your blog is relevant.
    Step 3:

    Participate in forums, where your blog is relevant, and include the URL.
    Step 4:
    Comment on other blogs, in relevant comments, and include the URL.
    Step 5:

               Put relevant and useful content in your blog.
    Step 6:
          Some search engines may index your blog using the site feed.  Be sure to activate the feed(check with Google webmaster tool).

    Step 7:
              When you have done all of the above, your blog will have some weight.  Submit your blog to the search engines, for indexing.
    Step 8:

        Post frequently, and keep the content of your blog moving.
    Step 9:
              Avoid your blog/website loading problem(It shouldn’t take more time to load).
    Step 10:
          Add your blog url in the following link.
    Step 11:
             Check your robots.txt file with Google webmaster tool.
    Step 12:
                   Wait for few days until you get a following message in Google webmaster tool.
    Googlebot has successfully accessed your home page.
    Pages from your site are included in Google's index.

    Tuesday, November 24, 2009

    Best Blog Widgets

    Widgets are designed to make your content more attractive and more enjoyable for your readers to consume.
    You can get the some good widgets from the following urls.
    Panoramic Slideshow
    Live Activity
    Photo Gallery
    Feed Content 
    Slideshow
    Tag Cloud
    How to get above widgets?
    You just send your blog url/website address to following address
    They will review your blog/website if satisfies their criteria then you
    will get link for the above widgets.

    Saturday, November 21, 2009

    Java and XML

    Following are the APIs for java with xml
    1.JDOM
    2.Java API for XML processing (JAXP)
    3.SAX Parser

    Useful links for java and xml

    Useful tools for blogger

    Google Webmasters tools

    Google Webmaster Tools is free web service tool by Google for webmasters. It allows webmasters to check indexing status and optimize visibility of their websites.
    Steps to use Google webmaster tool
    1.First we need to do Login for Google webmaster.
    2.Add your website/blog url using Add site button
    3.Submit a Sitemap to tell Google about pages on your site we might not otherwise discover
    Click Site Configuration section click sitemaps
    for bloggers
       atom.xml
    e.g
    or we can add site feed also
    e.g
    Crawl access:
    In this section we have robots.txt file to specify how search engines should crawl your site's content.
    We can specify whether search should search your site/blog or not.
    for unregistered bloggers can’t change their robot.txt file
    If you have any problem with your sitemap or your site pages then you will get some crawl errors that will be displaying in crawl errors section.
    If you don’t have any errors then after few days if googlebot successfully crawled your site/blog means you will get the below
    message.
    Googlebot has successfully accessed your home page.
    Pages from your site are included in Google's index.

    Google Analytics

     Google Analytics  is a free web service offered by Google that generates detailed statistics about the visitors to a website.
        

    GoogleAnalytics

    Windows Live Writer

    Writer Live Writer makes it easy to share your photos and videos on almost any blog service—Windows Live, Wordpress, Blogger, LiveJournal, TypePad, and many more.
         It is a development tool/platform for blogger.And It has lot of features to post the blog easily.   

    This update to Windows Live Writer 1.0 (Beta) features:

    • Tagging support
    • Support for Blogger Beta
    • Improvements to categories
    • And more...(details below)
          image

    Thursday, November 19, 2009

    Java - Card Layout

       Card Layout is used to manages number of panels(or components) in same dialog that share the same display space.
    For example,
     1.Next, Back, and Cancel buttons in the same dialog to see number of panels with same or different components.
     2.We can create installation wizards.


    Card Layout Example


    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;

    public class CardLayExample extends JFrame {
       
        private int currentCard = 1;
        private JPanel cardPanel;
        private CardLayout cl;
       
        public CardLayExample() {
           
            setTitle("Card Layout Example");
            setSize(300, 150);
            cardPanel = new JPanel();
           
            // getContentPane().add(cardPanel);
            cl = new CardLayout();
            cardPanel.setLayout(cl);
            JPanel jp1 = new JPanel();
            JPanel jp2 = new JPanel();
            JPanel jp3 = new JPanel();
            JPanel jp4 = new JPanel();
            JLabel jl1 = new JLabel("Label1");
            JLabel jl2 = new JLabel("
    Label2");
            JLabel jl3 = new JLabel("
    Label3");
            JLabel jl4 = new JLabel("
    Label4");
            jp1.add(jl1);
            jp2.add(jl2);
            jp3.add(jl3);
            jp4.add(jl4);
            cardPanel.add(jp1, "1");
            cardPanel.add(jp2, "2");
            cardPanel.add(jp3, "3");
            cardPanel.add(jp4, "4");
            JPanel buttonPanel = new JPanel();
            JButton firstBtn = new JButton("First");
            JButton nextBtn = new JButton("Next");
            JButton previousBtn = new JButton("Previous");
            JButton lastBtn = new JButton("Last");
            buttonPanel.add(firstBtn);
            buttonPanel.add(nextBtn);
            buttonPanel.add(previousBtn);
            buttonPanel.add(lastBtn);
           
            firstBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    cl.first(cardPanel);
                    currentCard = 1;
                }
            });
           
            lastBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    cl.last(cardPanel);
                    currentCard = 4;
                }
            });
           
            nextBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    if (currentCard < 4) {
                        currentCard += 1;
                        cl.show(cardPanel, "" + (currentCard));
                    }
                }
            });
           
            previousBtn.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent arg0) {
                    if (currentCard > 1) {
                        currentCard -= 1;
                        cl.show(cardPanel, "" + (currentCard));
                    }
                }
            });
           
            getContentPane().add(cardPanel, BorderLayout.NORTH);
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
        }
        public static void main(String[] args) {
            CardLayExample cl = new CardLayExample();
            cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            cl.setVisible(true);
        }
    }
     



     

    Bookmark and Share
    Hihera.com
    Increase Page Rank Google
    TopBlogDir.blogspot.com button
    Best Indian websites ranking
    Tips for New Bloggers
    TopOfBlogs
    The Link Exchange - Your ultimate resource for link exchange!

    About This Blog

    TopOfBlogs

    FEEDJIT Live Traffic Feed

      © Blogger template Webnolia by Ourblogtemplates.com 2009

    Back to TOP