Solved

integrating native code into java

Posted on 1997-05-05
4
311 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
[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
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…

734 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