This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JNI: I can't run the HelloWorld program successfully.  RSS feed

 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's the HellWorld program from Sun's The Java Native Interface
Programmer's Guide and Specification. I don't install a VC++ compiler in my machine. I find a way to comiple .c to .dll with gcc at:http://www.mingw.org/MinGWiki/index.php/JNI-MinGW-DLL

I get the dll successfully. When I run the HelloWorld class, no errors, but "Hello World!" isn't printed on the screen either. What's the problem?

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are issues with using stdio from JNI libraries on some platforms and with some libraries. I recall seeing this same problem quite a few years ago when using gcc to compile native methods on NT 4. I think the issue is that the JVM is compiled to use the stdio from the MSVCRT runtime, and of course gcc has its own runtime; there's some disagreement between the two about how file descriptors should be used. I wouldn't worry about this too much; it's the price you pay for working on windows without shelling out for Microsoft's toolchain.

Anyway, try an example where your native method returns a value, and print the value from Java.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That shouldn't be necessary. I've used MinGW with JNI as well and I had no problems whatsoever printing data.

Louis, can you please show us your C code?
 
Qunfeng Wang
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your replies.
I install a cygwin in my XP machine. I don't know if a MinGW has been installed. I just couldn't get the file compiled at the beginning, the error is:

I tried that command from MinGWWiki and luckily compiled successfully.

Ernest, I tried another example as you suggested. I don't get any result when running the program either. I don't think some thing wrong with the code, I just download them from the website. I will paste the code below for your reference.
1. IntArray.c


2IntArray.java
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Louis Wang:
Thank you for your replies.
I install a cygwin in my XP machine. I don't know if a MinGW has been installed. I just couldn't get the file compiled at the beginning, the error is:

I tried that command from MinGWWiki and luckily compiled successfully.

The _WinMain error is because you haven't specified that it should be a shared library, so a main method was expected. You didn't have one so this error indicated there was no valid entry point.

It's similar to Java actually. Unless you specify that it should be a shared library, a function similar to static void main is needed:
- int main(), or
- int main(int argc, char** argv)


I tried your IntArray example with an added print statement:

The result:
Result until 0: 0
Result until 1: 1
Result until 2: 3
Result until 3: 6
Result until 4: 10
Result until 5: 15
Result until 6: 21
Result until 7: 28
Result until 8: 36
Result until 9: 45
sum = 45

As you can see, no problems there.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!