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

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.
jgdvishnuSoftware ArchitectAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Amitkumar PSr. ConsultantCommented:
Gary PattersonVP Technology / Senior Consultant Commented:
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:


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


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.
Is there any exquisite functionality in native library that is absolutely unreachable via JAVA API (including option to get to Java 7?) ?
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Gary PattersonVP Technology / Senior Consultant Commented:

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.
Well ermm in typical situation nobody gains from listing files 3x faster but leaving 100k mem leak after...
Gary PattersonVP Technology / Senior Consultant Commented:
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.
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....
Gary PattersonVP Technology / Senior Consultant Commented:

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.
Do you have C compiler on AIX ?
By default you need to install gcc or buy a licence for included xlc...
jgdvishnuSoftware ArchitectAuthor Commented:
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.
jgdvishnuSoftware ArchitectAuthor Commented:
@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.
Gary PattersonVP Technology / Senior Consultant Commented:
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:


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


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.

You have xlc installed and trial is usually expired by the time you want to use it, so best to stick with gcc...
jgdvishnuSoftware ArchitectAuthor Commented:
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 .

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jgdvishnuSoftware ArchitectAuthor Commented:
This issue cleared lots of doubts
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Unix OS

From novice to tech pro — start learning today.