Solved

looking for example of C based Java Native method that can run on AIX (64 bit) with Java 1.5

Posted on 2014-02-28
15
755 Views
Last Modified: 2014-03-09
looking for bug free - simple JNI example that could run on

Native Language C
OS = AIX (64 bit)
Java 1.5 (IBM J9 VM)

Java Native method should take a string as input and return String as output.

I saw different examples on internet but always come across examples that did not work.
0
Comment
Question by:jgdvishnu
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 20

Expert Comment

by:Amitkumar Panchal
ID: 39897690
0
 
LVL 34

Assisted Solution

by:Gary Patterson
Gary Patterson earned 250 total points
ID: 39897718
Don't know why doing this on AIX would be any different that anyplace else.  Chapter 4.2 of this shows a string-passing example:

http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

javah tools is used to generate C skeleton from a Java Native Method interface declaration:

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/javah.html

Or, if you have an existing C program and want to generate a Java interface, you can use SWIG (http://www.swig.org/Doc2.0/Java.html) to generate the C and Java code needed to interface to an existing C module.

Either way, you would end up with the example code you want - just start with either the Java or the C and use the appropriate tool to generate the significant code.
0
 
LVL 61

Expert Comment

by:gheist
ID: 39897823
Is there any exquisite functionality in native library that is absolutely unreachable via JAVA API (including option to get to Java 7?) ?
0
 
LVL 34

Expert Comment

by:Gary Patterson
ID: 39897880
@gheist:

Of course.  That's what Java Native Methods are for.  Every platform has functionality that isn't covered specifically by Java, and ultimately low-level functions end up making native calls at some point - especially device IO.

Java is a "compromise" language developed to run in an imaginary computer.  Implementation on various platforms eventually requires numerous mapping to native functions.

You can also use JNM to interface to libraries of custom code, package software, etc.  Impractical to rewrite all code ever written in a native language to Java.
0
 
LVL 61

Expert Comment

by:gheist
ID: 39897896
Well ermm in typical situation nobody gains from listing files 3x faster but leaving 100k mem leak after...
0
 
LVL 34

Expert Comment

by:Gary Patterson
ID: 39897941
If you ask me, there is no "typical situation".

When I do application architecture, I take into account a lot of factors:  current and future requirements, performance needs, organizational goals, timeline, budget, development team skill level, best practices, and shop standards.  Then I pick the best tools for the situation:  understanding that the best decision is often a compromise between performance, cost, timeline, functionality, and portability.

And once you've decided on the platforms, tools and techniques, then it is a good idea to make sure the coding is done by competent programmers who know enough not to create a memory leak in the first place - and then follow good testing procedures to make sure.  Even the best programmers make mistakes sometimes.

Bad programmers can and will screw up in any programming language.  

I know I've fixed plenty of memory problems in poorly-written Java that didn't have anything to do with native methods.
0
 
LVL 61

Expert Comment

by:gheist
ID: 39897958
Hopw you agree would be quite a waste of time reimplementing something in JNI for 1.5 when +/- same is ready baked in Java  7....
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 34

Expert Comment

by:Gary Patterson
ID: 39897979
@gheist,

That's a little abstract for me.  I can think of cases where I might code a native method when similar functionality exists in Java (any version)- performance comes to mind.  It just depends on the situation.

In this thread, I'm going to stick to dealing with the poster's question, which was a specific technical question related to Java 1.5.

If you have a question of your own, please open a new question, though - perhaps someone else has some useful insights to share.
0
 
LVL 61

Expert Comment

by:gheist
ID: 39898222
Do you have C compiler on AIX ?
By default you need to install gcc or buy a licence for included xlc...
0
 

Author Comment

by:jgdvishnu
ID: 39898488
Yes. I could run some of gcc commands.
Some of gcc commands in example not run. Can you please let me guide through gcc tutorial what each options mean and why that option is required.
0
 

Author Comment

by:jgdvishnu
ID: 39898601
@Amitkumar Panchal
It listed steps like:-
training-> javah -stubs Happy
Error: JNI does not require stubs, please refer to the JNI documentation.

Open in new window

I have not seen paths like "java/include/genunix" especially in Java 5

gcc -shared -Wl,-soname,libhappy.so.1 -o libhappy.so.1.0 HappyImp.o

ld: 0706-005 Cannot find or open file: libhappy.so.1
        ld:open(): A file or directory in the path name does not exist.

Open in new window

This is the problem with most tutorial that they miss some steps and it becomes nightmare for newbees.

Swig works on windows and I do not have permission to install on AIX box.
I need understanding on gcc commands as I am not clear when some compiler shows up.
0
 
LVL 34

Assisted Solution

by:Gary Patterson
Gary Patterson earned 250 total points
ID: 39898715
As you've probably figured out, JNI native methods isn't really a good "beginner" topic.

The tutorial you are following shows a very old, pre-JNI way of creating native methods.  It is way out-of-date.

Did you notice the date on the article?  If it is accurate, then this is from 1997, that's back in the days of Java 1.1!

When you run into a problem like this, take a few minutes to look up the error message, or look up the command .  For example, look at the docs for Java 1.5 javah:

http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javah.html

It says, in part:

"By default javah creates a header file for each class listed on the command line and puts the files in the current directory. Use the -stubs option to create source files. Use the -o option to concatenate the results for all listed classes into a single file.

The new native method interface, Java Native Interface (JNI), does not require header information or stub files. javah can still be used to generate native method function proptotypes needed for JNI-style native methods. javah produces JNI-style output by default, and places the result in the .h file."

This is a hint that you need to look for a tutorial or resources for JNI native methods.

I'm not an AIX programmer, though I occasionally have to compile or modify an AIX program for use on IBM i.  I've never written a JNI native method on AIX, but I'll try to provide some guidance:

Here is a very simple JNI native method example on AIX - you''l need cc_r, which is part of  Xlc.  If you don't have it, you can download a trial from

http://www-03.ibm.com/software/products/en/xlcpp-aix/

If you want to follow a tutorial that uses gcc, it looks like you'll need to create an AIX shared native library for the C, and force export of the mangled symbols created by javah.  That means you'll need to specify the gcc -shared -bexpfull options.

http://www.ibm.com/developerworks/aix/library/au-gnu.html
0
 
LVL 61

Expert Comment

by:gheist
ID: 39898764
You have xlc installed and trial is usually expired by the time you want to use it, so best to stick with gcc...
0
 

Accepted Solution

by:
jgdvishnu earned 0 total points
ID: 39905626
Thanks gheist:- I sticked to gcc and that paid off.
The gcc command that worked on the AIX6.1 (64 bit was)

gcc -o libAnyName.so   -shared -maix64 -I/usr/java5/include com.pkg.JavaClassName.c -lc

Open in new window



libAnyName.so was referred in java class by absolute path using System.load("/absolute/path/of/libAnyName.so");

maix64 - this referred to creating 64 bit shared object. Ever since I added to the command I got rid of this error for ever.
ld: 0706-005 Cannot find or open file: libhappy.so.1 ld:open(): A file or directory in the path name does not exist.

I read most of the relevant examples of and they worked.
Java Programming Tutorial Java Native Interface (JNI)

@Amit Commands like "-stubs" in example were not needed.
javah -stubs Happy

@gheist - need of this project work - There are some third party "C" code that never talk to SAP. Using JNI it seems it could be connected easily and efficiently .
0
 

Author Closing Comment

by:jgdvishnu
ID: 39915660
This issue cleared lots of doubts
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now