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?

Improve company productivity with a Business Account.Sign Up

x
 
jgdvishnuConnect With a Mentor Software 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 .
0
 
Amitkumar PSr. ConsultantCommented:
0
 
Gary PattersonConnect With a Mentor VP 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:

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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
gheistCommented:
Is there any exquisite functionality in native library that is absolutely unreachable via JAVA API (including option to get to Java 7?) ?
0
 
Gary PattersonVP Technology / Senior Consultant Commented:
@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
 
gheistCommented:
Well ermm in typical situation nobody gains from listing files 3x faster but leaving 100k mem leak after...
0
 
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.
0
 
gheistCommented:
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
 
Gary PattersonVP Technology / Senior Consultant Commented:
@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
 
gheistCommented:
Do you have C compiler on AIX ?
By default you need to install gcc or buy a licence for included xlc...
0
 
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.
0
 
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.
0
 
Gary PattersonConnect With a Mentor VP 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:

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
 
gheistCommented:
You have xlc installed and trial is usually expired by the time you want to use it, so best to stick with gcc...
0
 
jgdvishnuSoftware ArchitectAuthor Commented:
This issue cleared lots of doubts
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.