Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > JNI Issue

Reply
Thread Tools

JNI Issue

 
 
ABC
Guest
Posts: n/a
 
      07-04-2005
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

 
Reply With Quote
 
 
 
 
Stefan Schulz
Guest
Posts: n/a
 
      07-04-2005
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"


 
Reply With Quote
 
 
 
 
ABC
Guest
Posts: n/a
 
      07-04-2005
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"


 
Reply With Quote
 
Daniel Dyer
Guest
Posts: n/a
 
      07-04-2005
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
 
Reply With Quote
 
Stefan Schulz
Guest
Posts: n/a
 
      07-04-2005
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"


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JNI thread vs. native thread vs. JNI call bgabrhelik Java 0 09-29-2009 03:33 PM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:03 AM
Re: JNI: Error loading DLL from JNI DDL vasanth Java 0 01-25-2005 11:01 AM
Porting JNI Windows under JNI LINUX + Wine ? Pasturel Jean-Louis Java 5 03-03-2004 07:50 PM
IBM's JNI fails where Sun's JNI works Alex Hunsley Java 4 11-04-2003 10:34 AM



Advertisments