6

I have an application in C/C++ that writes data in shared memory in Unix. Now I want to read that shared memory data through a program using Java.

The program should read the shared memory data. Can it be possible?

Need a small implementation if possible.

4
  • 1
    Reading shared memory does if ever only work through JNI - in Java you can't do that. My experience with JNI was that some JNI-plugins crashed the otherwise rock-solid Java VM. So maybe another solution is possible? Commented Jan 8, 2013 at 8:38
  • This answer stackoverflow.com/a/1492536/1741542 talks about memory mapped files and NIO FileChannel. Maybe this works for you too. Commented Jan 8, 2013 at 8:54
  • If you would benefit from persistence of the data (even for debugging purposes) you could consider memory mapped files. Here an implementation which uses pure Java github.com/peter-lawrey/Java-Chronicle Commented Jan 8, 2013 at 9:14
  • @luukes Are you sure that using a Java program we cannot read the shared memory data written by c++ program? And we have to have JNI in between to achieve this? Commented Jan 9, 2013 at 11:10

1 Answer 1

5

Take a look at this codeproject project (For Windows):

Using Memory Mapped Files and JNI to communicate between Java and C++ programs

Or, this library:

CLIPC is an open-source Java library that gives developers interprocess communications (IPC) capabilities that may be absent or difficult to use in the Java distribution.

Sign up to request clarification or add additional context in comments.

3 Comments

Can I create a method in C++ only to read the shared memory, and then call this method using JNI? Would that be effective and won't harm to performance also?
JNI methods should call from Java not C++. Yes you can create a method in C++ to read/write from/to the shared memory. And also you can do same thing in Java with JNI methods. Don't worry about performance if you do it right.
Can you please just tell me the suggestions how can I achieve the below functionality?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.