File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes Java and C++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Java and C++" Watch "Java and C++" New topic
Author

Java and C++

Gaurav Pawar
Ranch Hand

Joined: Sep 12, 2006
Posts: 37
hi all,
I am new to JNI. I have an existing C++ header file and a cpp file taht uses the header file. I need to write a Java wrapper for this.

Can you please help me by posting links where i can get actual code which can demo how to do this? or i would also very much appreciate help if anyone can give me actual code where say a C++ object instance is being created by calling Java methods or something similar!!!

Thanks a lot... i need a code example....

Regards,
Gaurav
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14146
    
  18

Have you tried Google, search for "java native interface tutorial".

Found this in less than 10 seconds:
Using the Java Native Interface with C++
JNI Technology

etc...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Here is a link to the old Sun tutorial on the Java Native Interface. I don't know why they don't have it posted anymore as browseable HTML -- it's only available as this downloadable ZIP file. But it's an excellent step-by-step tutorial that will explain all you need to know.


[Jess in Action][AskingGoodQuestions]
Gaurav Pawar
Ranch Hand

Joined: Sep 12, 2006
Posts: 37
Hey thanks a lot for the prompt reply.
I have already been thru most of these links. All these codes involve modification of C++ code.

What if i do not want to touch my C++ code at all and write a JNI wrapper to it? I mean is it possible to call C++ modules without having to modify its signature and keeping the C++ code as it is?

Thanks
Gaurav
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

You can leave your C++ code untouched only if you write more C++ code to "wrap" it with. To call C/C++ code from Java, there needs to be some C/C++ "glue" code. You can write this yourself, or pay for a commercial product which does it automatically for you.
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
[hijack]I've been wondering this lately. Is there a way we can make a JNI dll in VB? You can write a dll in VB, right? and all we need to do is make sure that the VB dll exports functions in the way Java likes it. That might make it easier on people who don't know C++ [/hijack]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Originally posted by Jayesh Lalwani:
[hijack]I've been wondering this lately. Is there a way we can make a JNI dll in VB? You can write a dll in VB, right? and all we need to do is make sure that the VB dll exports functions in the way Java likes it. That might make it easier on people who don't know C++ [/hijack]


I imagine it might be possible, as long as (a) VB gives you control over the exact names of the symbols you export, and (b) the VB runtime (which nowadays I guess is the .NET CLI!) is amenable to being embedded in the same process with a JVM.

I suspect the second requirement is less likely than the first!
Jayesh Lalwani
Ranch Hand

Joined: Nov 05, 2004
Posts: 502
I admit I have been out of the Microsoft world for a long time now, so I might be off the mark, but I distinctly remember VB developers claiming that they can write DLL's just like C++ developers can. Basically, the claim was that VB can do anything that C++ can.

I don't think b) is an issue because you don't really need a VB "runtime" to use a VB dll. The compiler could either statically link all the VB functions into your DLL (creating a huge dll), or dynamically link your dlls to VB dlls that have to be shipped with your dll (which leads to DLL hell). I beleive, more recently, Microsoft has been claiming that all their languages have the same capabilities, but I haven't looked too much into it. I am curious whether you can still create dll's using any of the Microsoft languages

I think a) might be an issue, because there are so many ways you can export symbols out of DLLs, and Java prefers one way, but I don't know if VB compiler implements that.

Alternatively, we can get around a) by creating a COM object in VB and using JACOB to interface Java to VB COM dll. This would introduce too many layers, and would add a learning curve for COM.

Ideally, I would love to see a javah type of utility that takes a native class and creates a .bas file from it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java and C++