Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

integrating native code into java

Posted on 1997-05-05
4
Medium Priority
?
318 Views
Last Modified: 2010-04-16
I am not sure if this is a C question or java.  Anyhow I have been reading the tutorial at

http://java.sun.com/tutorial/native1.1/stepbystep/index.html

and I have followed it exactly except I had to make a minor change to the cc command as noted below.

Anyhow I compile all the java code and then the C code but when I run it I get the following.

heracles 56% java Main
 4450:/usr/java/bin/../bin/sgi/green_threads/java: rld: Fatal Error: cannot successfully map soname 'libhello.so' under any of the filenames /usr/people/mv135329/java/native/libhello.so:/usr/java/bin/../lib/sgi/green_threads/libhello.so:/usr/lib/libhello.so:/lib/libhello.so:/lib/cmplrs/cc/libhello.so:/usr/lib/cmplrs/cc/libhello.so:  (libhello.so)
java.lang.UnsatisfiedLinkError: no hello in shared library path
        at java.lang.Runtime.loadLibrary(Runtime.java:440)
        at java.lang.System.loadLibrary(System.java:569)
        at
        at Main.main(Main.java:3)

according to the tutorial (step 6) I need set the library path, I did that and that had no effect.  You can see in the error message that that it is looking in the correct directory '...java/native' for the library called libhello.so.  This file is in that directory.  So this makes me wonder if I am creating the library wrong?? Here is the command I am using to do it with

'cc -I/usr/java/include -I/usr/java/include HelloWorldImp.c -o libhello.so -c'

I had to change it a bit from the tutorial.  No matter what I seem to do I always get this message.  I am assuming I am not creating the library properly.  If this is the case this might be a question for the C experts, but I am not sure.

Thanks
0
Comment
Question by:micah
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 50 total points
ID: 1220146
Librarys in UNIX generaly carry a .a  (dot a) extetiion. The -l option assumes that the library will have that extention, so you command should look something like:

cc -I/usr/java/include -I/usr/java/include HelloWorldImp.c -o SomeName -llibhello.so -c

You would need to have /usr/java/lib in your path and you would require libhello.so to be called libhello.so.a

The tutorial is not really verry good for native code. I would suggest you look at:

http://www.mcp.com/que/et/se_java2e/41javafi.htm

for much better coverage.

Note 1: libhello.so is not a standard java  lib. Check what your doing carefully, it has to be one of the libs in /usr/java/lib.

Note 2: Save the URL contents of the link I sent you since these  pages appear and disappear without notice.
0
 

Author Comment

by:micah
ID: 1220147
Followed that tutorial, with even stanger results.  To compile the C code on a sun the tutorial says use the makefile which looks to do the same cc command I was using.  When ever I use the -G I get

ld: ERROR 33: Unresolved data symbol "main" -- 1st referenced by /usr/lib/crt1.o.
ld: INFO 60: Output file removed because of error.

so instead of -G I use -c, that would at least generate the .so file.  For the above tutorial when I do

cc -I/usr/java/include -I/usr/java/include/irix HelloJavaImp.c HelloJava.c -o libhello.so -c

I get no errors but also no output file or any message.

I am a little confused about your above statement.  Both the tutorials generate a file called libhello.so.  The errors I get seem to be looking for libhello.so, so what is the '.a'  
0
 
LVL 6

Expert Comment

by:jpk041897
ID: 1220148
The -o compiler flag means that you want the output generated by the compiler to be called libhello.so.

For instance:

cc myprog.c

prduces a.out as output

cc myprog.c -o myprog.exe

produces myprog.exe as output

and

cc HelloJavaImp.c HelloJava.c -o libhello.so

will produce an oyput file called libhello.so

Fact of the matter is, your compile command line is not linking any libraries except for the standar C libs. If you are using jacvah and/or javah -stubs code in your source, you are going to have to link in the java libraries.

The Java libs set up a series of callbacks that allow the Java VM to talk to your app. The code you are generating does not include those callbacks and therfore cannot establish communication at run time.
0
 

Author Comment

by:micah
ID: 1220149
Ok, I found out how to do this elsewhere just had to add a
'-shared' flag to the cc command line and eveything works now.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

783 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