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
792 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +1
15 Comments
 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 39897690
0
 
LVL 35

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 62

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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 35

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 62

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 35

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 62

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
 
LVL 35

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 62

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 35

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 62

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

623 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