JNI Issue

Discussion in 'Java' started by ABC, Jul 4, 2005.

  1. ABC

    ABC Guest

    I am trying to acccess variables from My C++ program ( In Linux ). My
    C++ program is running as a seprate process . When I try to access the
    variable from my Java program i am always getting the initialised
    value.

    Eg.
    test.c
    int count = 0;
    func1()
    {
    while(1)
    {
    count++;
    sleep(1);
    }
    }
    get()
    {
    printf("Count %d\n",count );
    }

    main.c
    main()
    {
    func1();
    }

    Now i compiled the file test.c with my Native .c file and created the
    test.so file .
    Then I linked main.c with test.so and created executable file.

    Run the executable file and try calling the function get from Java
    through JNI interface , But everytime i am getting value 0

    Can anybody please tell me whether i am doing it correctly or not , My
    objective is to access the runtime C++ variable value from Java and
    display it on GUI.

    Thanks
    ABC, Jul 4, 2005
    #1
    1. Advertising

  2. On Mon, 04 Jul 2005 07:06:00 -0700, ABC wrote:

    > Run the executable file and try calling the function get from Java
    > through JNI interface , But everytime i am getting value 0


    this is probably because your main never gets called. You are creating a
    library, after all. All JNI does is load that library into memory, and
    then access the variable. Your func1 is never called. Your main is never
    called.

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
    Stefan Schulz, Jul 4, 2005
    #2
    1. Advertising

  3. ABC

    ABC Guest

    But when I run the a.out formed by compiling the main.c with test.so
    and running the a.out my main is called.



    Stefan Schulz wrote:
    > On Mon, 04 Jul 2005 07:06:00 -0700, ABC wrote:
    >
    > > Run the executable file and try calling the function get from Java
    > > through JNI interface , But everytime i am getting value 0

    >
    > this is probably because your main never gets called. You are creating a
    > library, after all. All JNI does is load that library into memory, and
    > then access the variable. Your func1 is never called. Your main is never
    > called.
    >
    > --
    > You can't run away forever,
    > But there's nothing wrong with getting a good head start.
    > --- Jim Steinman, "Rock and Roll Dreams Come Through"
    ABC, Jul 4, 2005
    #3
  4. ABC

    Daniel Dyer Guest

    JNI is for combining Java and C/C++ in a single process. What you have is
    two processes one in Java with JNI and another in C++. Each has its own
    separate copy of the variable. They are completely unrelated and changes
    to one will not be reflected in the other.

    If you want two separate programs to talk to each other JNI is not
    necessary. You could use a socket connection between the two, write to a
    temporary file or use something like CORBA (probably over-kill).

    Dan.

    On Mon, 04 Jul 2005 15:06:00 +0100, ABC <> wrote:

    > I am trying to acccess variables from My C++ program ( In Linux ). My
    > C++ program is running as a seprate process . When I try to access the
    > variable from my Java program i am always getting the initialised
    > value.


    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Jul 4, 2005
    #4
  5. On Mon, 04 Jul 2005 07:21:29 -0700, ABC wrote:

    > But when I run the a.out formed by compiling the main.c with test.so
    > and running the a.out my main is called.
    >


    When is the main of libc.so ever called? ;)

    Of course your main is called if you ask the operating system to do just
    that (by invoking your program). However, by loading your library, you ask
    the OS to do something different, namely load the library, and prepare it
    to have its functions by some already existing process. (the loading
    process to be precise)

    --
    You can't run away forever,
    But there's nothing wrong with getting a good head start.
    --- Jim Steinman, "Rock and Roll Dreams Come Through"
    Stefan Schulz, Jul 4, 2005
    #5
    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. Alex Hunsley

    IBM's JNI fails where Sun's JNI works

    Alex Hunsley, Nov 3, 2003, in forum: Java
    Replies:
    4
    Views:
    847
    Alex Hunsley
    Nov 4, 2003
  2. Pasturel Jean-Louis

    Porting JNI Windows under JNI LINUX + Wine ?

    Pasturel Jean-Louis, Feb 29, 2004, in forum: Java
    Replies:
    5
    Views:
    902
    Pasturel Jean-Louis
    Mar 3, 2004
  3. vasanth
    Replies:
    0
    Views:
    2,685
    vasanth
    Jan 25, 2005
  4. vasanth
    Replies:
    0
    Views:
    620
    vasanth
    Jan 25, 2005
  5. bgabrhelik
    Replies:
    0
    Views:
    802
    bgabrhelik
    Sep 29, 2009
Loading...

Share This Page