• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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.
 
I was her plaything! And so was this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic