Solved

integrating native code into java

Posted on 1997-05-05
4
308 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 25 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
recursion example 16 125
difference of if loops 23 49
by zero exception 10 39
ejb on wildfly 5 19
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

776 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