Accessing Windows API

Discussion in 'Java' started by Ken Kast, Apr 11, 2004.

  1. Ken Kast

    Ken Kast Guest

    I have the following code in my Main class:

    public class Main {
    static {
    System.loadLibrary("kernel32");
    }
    native private static short GetSystemDefaultLangID();

    and in main I have

    ID = GetSystemDefaultLangID(); // Get Language ID for this system

    When it executes this line in debug mode the following exception is
    thrown:

    java.lang.UnsatisfiedLinkError: GetSystemDefaultLangID
    at Main.GetSystemDefaultLangID(Native Method)
    at Main.main(Main.java:92)
    All the right directories are in the PATH variable.

    I am teaching myself Java and this app is a porting of a working one
    originally written in VB .NET, so I know the underlying code is OK.

    Can anyone give me a hint what I'm doing wrong?

    Thanks.
     
    Ken Kast, Apr 11, 2004
    #1
    1. Advertising

  2. Ken Kast wrote:

    > I have the following code in my Main class:
    >
    > public class Main {
    > static {
    > System.loadLibrary("kernel32");
    > }
    > native private static short GetSystemDefaultLangID();
    >
    > and in main I have
    >
    > ID = GetSystemDefaultLangID(); // Get Language ID for this system
    >
    > When it executes this line in debug mode the following exception is
    > thrown:
    >
    > java.lang.UnsatisfiedLinkError: GetSystemDefaultLangID
    > at Main.GetSystemDefaultLangID(Native Method)
    > at Main.main(Main.java:92)
    > All the right directories are in the PATH variable.
    >
    > I am teaching myself Java and this app is a porting of a working one
    > originally written in VB .NET, so I know the underlying code is OK.
    >
    > Can anyone give me a hint what I'm doing wrong?
    >
    > Thanks.


    Calling native methods doesn't work like that. You have to adapt/wrap the
    native library to be JNI (Java Native Interface) compatible.

    Look at http://java.sun.com/docs/books/tutorial/native1.1/ for a tutorial on
    JNI.

    --
    Kind regards,
    Christophe Vanfleteren
     
    Christophe Vanfleteren, Apr 11, 2004
    #2
    1. Advertising

  3. Ken Kast

    Ken Kast Guest

    I followed the tutorial you cite in devising the code, as well as
    looking at posted examples/questions on the web. I saw examples of
    the direct loading of Windows libraries. It makes no sense that one
    has to wrap a Windows library in order to use it in Java. Why would I
    write C code to wrap C?

    Ken

    Christophe Vanfleteren <> wrote in message news:<fghec.68098$-ops.be>...
    > Ken Kast wrote:
    >
    > > I have the following code in my Main class:
    > >
    > > public class Main {
    > > static {
    > > System.loadLibrary("kernel32");
    > > }
    > > native private static short GetSystemDefaultLangID();
    > >
    > > and in main I have
    > >
    > > ID = GetSystemDefaultLangID(); // Get Language ID for this system
    > >
    > > When it executes this line in debug mode the following exception is
    > > thrown:
    > >
    > > java.lang.UnsatisfiedLinkError: GetSystemDefaultLangID
    > > at Main.GetSystemDefaultLangID(Native Method)
    > > at Main.main(Main.java:92)
    > > All the right directories are in the PATH variable.
    > >
    > > I am teaching myself Java and this app is a porting of a working one
    > > originally written in VB .NET, so I know the underlying code is OK.
    > >
    > > Can anyone give me a hint what I'm doing wrong?
    > >
    > > Thanks.

    >
    > Calling native methods doesn't work like that. You have to adapt/wrap the
    > native library to be JNI (Java Native Interface) compatible.
    >
    > Look at http://java.sun.com/docs/books/tutorial/native1.1/ for a tutorial on
    > JNI.
     
    Ken Kast, Apr 12, 2004
    #3
  4. On 12 Apr 2004 07:24:57 -0700, Ken Kast wrote:
    > I followed the tutorial you cite in devising the code, as well as
    > looking at posted examples/questions on the web. I saw examples of
    > the direct loading of Windows libraries. It makes no sense that one
    > has to wrap a Windows library in order to use it in Java. Why would
    > I write C code to wrap C?


    Because you can't invoke arbitrary C functions from java. With JNI you
    can call methods that:

    - are declared in some java class
    - follow a very specific naming and calling convention

    For example, do the methods you are trying to call expect a JNIEnv* as
    the first argument, or an Object reference as the second argument?

    You can write a Java class containing native methods that follow the
    JNI convention. From those methods you can call any existing functions
    you like.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Apr 12, 2004
    #4
  5. Ken Kast

    Jim Cobban Guest

    "Ken Kast" <> wrote in message
    news:...
    > I have the following code in my Main class:
    >
    > public class Main {
    > static {
    > System.loadLibrary("kernel32");
    > }
    > native private static short GetSystemDefaultLangID();
    >
    > and in main I have
    >
    > ID = GetSystemDefaultLangID(); // Get Language ID for this system


    I am presuming that you used this simply as an example of the sort of Win32
    function you wished to call. That is because if all you want is the default
    language ID for your system it is preferable to get that from the default
    Locale object. The implementation of Locale obtains that information from
    Windows for you.

    import java.util.Locale;
    ....
    String langCode;
    langCode = Locale.getDefault().getLanguage()
     
    Jim Cobban, Apr 13, 2004
    #5
  6. Ken Kast

    Ian Shef Guest

    Gordon Beaton <> wrote in news:407acb3e$:

    <SNIP>
    >
    > You can write a Java class containing native methods that follow the
    > JNI convention. From those methods you can call any existing functions
    > you like.
    >
    > /gordon
    >


    .... or you can purchase a commercial library that provides the wrappers for
    you, already made, debugged, and tested. One example is xFunction (
    http://www.excelsior-usa.com/xfunction.html ), but there are several others.

    .... or you can find a free implementation, such as SWIG ( http://www.swig.org
    and especially http://www.swig.org/Doc1.3/Java.html#n2 ).

    I have not tried either of these myself.

    Try an altavista or google search using:
    jni AND java AND wrapper AND windows



    --
    Ian Shef 805/F6 * These are my personal opinions
    Raytheon Company * and not those of my employer.
    PO Box 11337 *
    Tucson, AZ 85734-1337 *
     
    Ian Shef, Apr 14, 2004
    #6
  7. <SNIP>
    >
    > I have not tried either of these myself.
    >
    > Try an altavista or google search using:
    > jni AND java AND wrapper AND windows



    Do not miss JNIWrapper (http://www.jniwrapper.com)
    You may even find that Windpws API functions that you need are already
    wrapped inside WinPack (a free addition to JNIWrapper)

    Good luck

    Eugene Toporov
     
    Eugene Toporov, Apr 15, 2004
    #7
  8. Ken Kast

    Guest

    peace.
    ---HelloWorld.java

    public class HelloWorld { public native short displayHelloWorld(); static { System.loadLibrary("hello"); }

    }
    ---

    javac HelloWorld.java
    javah -jni HelloWorld

    ---HelloWorldImp.c

    #include <stdio.h>#include <stdlib.h>#include <jni.h>#include "HelloWorld.h"
    #include <windows.h>
    JNIEXPORT jshort JNICALL Java_HelloWorld_displayHelloWorld
    (JNIEnv *env, jobject obj)
    { return GetSystemDefaultLangID();}
    ---

    gcc -g -O2 -c -Ic:/j2sdk1.4.2_03/include -Ic:/j2sdk1.4.2_03/include/win32 -g HelloWorldImp.c
    dllwrap --output-def hello.def --add-stdcall-alias -o hello.dll -s HelloWorldImp.o

    ---Main.java

    class Main { public static void main(String[] args) { short value = new HelloWorld().displayHelloWorld();
    System.out.println("ID: " +value); }}
    ---

    javac Main.java
    java -Djava.library.path=. Main

    Have a good day..
     
    , Apr 19, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Shlomo Anglister
    Replies:
    1
    Views:
    409
    Default User
    Aug 2, 2004
  2. Praveen, Tayal (IE10)
    Replies:
    0
    Views:
    378
    Praveen, Tayal (IE10)
    Mar 17, 2005
  3. John123

    Profiling API or Membership API

    John123, Oct 20, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    375
    John123
    Oct 20, 2006
  4. George2

    platform specific API or C standard API

    George2, Nov 12, 2007, in forum: C Programming
    Replies:
    13
    Views:
    752
    Tor Rustad
    Nov 13, 2007
  5. Replies:
    2
    Views:
    148
    bbiker
    Oct 25, 2007
Loading...

Share This Page