Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java and C++

 
Gaurav Pawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15272
37
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gaurav Pawar
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 502
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic