Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3312584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:00:46+00:00 2026-05-17T22:00:46+00:00

I’m using Ubuntu 10.10 So that’s what I did. Hello.java : class Hello {

  • 0

I’m using Ubuntu 10.10

So that’s what I did.

Hello.java:

class Hello {
        public native void sayHello();

        static { System.loadLibrary("hellolib"); }

        public static void main(String[] args){
                Hello h = new Hello();
                h.sayHello();
        }
}

Then I ran the follwing commands:

dierre@cox:~/Scrivania/provajni$ javac Hello.java

dierre@cox:~/Scrivania/provajni$ javah -jni Hello 

I’ve obtained Hello.class and Hello.h.

Hello.h:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Hello */

#ifndef _Included_Hello
#define _Included_Hello
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     Hello
 * Method:    sayHello
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_Hello_sayHello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif

Then I created Hello.cpp:

#include <jni.h>
#include "Hello.h"
#include  <iostream>

using namespace std;

JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *env, jobject obj) {
        cout << "Hello World!" << endl;
        return;
}

And now the part where I think I screwed up. I was inspired by this guide (Compile the Dynamic or Shared Object Library section):

dierre@cox:~/Scrivania/provajni$ gcc -I"/usr/lib/jvm/java-6-sun/include" -I"/usr/lib/jvm/java-6-sun/include/linux" -o hellolib.so -shared -Wl,-soname,hello.so Hello.cpp -static -lc

that generates the file hellolib.so

But when I try to run it with java Hello I have this error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no hellolib in java.library.path
 at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
 at java.lang.Runtime.loadLibrary0(Runtime.java:823)
 at java.lang.System.loadLibrary(System.java:1028)
 at Hello.<clinit>(Hello.java:4)
Could not find the main class: Hello.  Program will exit.

I even tried this:

  LD_LIBRARY_PATH=`pwd`
  export LD_LIBRARY_PATH

with no results.

I know I’m doing something extremely stupid but I can’t figure out what it is. The dynamic lib is generated with the -shared option, isn’t it?

Update #1

I tried static { System.load("/home/dierre/Scrivania/provajni/hellolib.so"); } to see if that worked but now:

Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/dierre/Scrivania/provajni/hello.so: /home/dierre/Scrivania/provajni/hello.so: undefined symbol: _ZSt4cout
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1699)
    at java.lang.Runtime.load0(Runtime.java:770)
    at java.lang.System.load(System.java:1003)
    at Hello.<clinit>(Hello.java:4)

Update #2
Ok, to solve the Update #1 problem I had to use g++ insted of gcc, obviously. Still having trouble to use the load method though. I can’t seem to tell it the right path.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T22:00:46+00:00Added an answer on May 17, 2026 at 10:00 pm

    Native library can be loaded by loadLibrary with a valid name. By example, libXXXX.so for linux family, your hellolib.so should rename to libhello.so.
    By the way, I develop java with jni, I will separate the implementation and native interface (.c or .cpp).

    static {
        System.loadLibrary("hello"); // will load libhello.so
    }
    

    The implementation header(HelloImpl.h):

    #ifndef _HELLO_IMPL_H
    #define _HELLO_IMPL_H
    
    #ifdef __cplusplus
            extern "C" {
    #endif
    
            void sayHello ();
    
    #ifdef __cplusplus
            }
    #endif
    
    #endif
    

    HelloImpl.cpp:

    #include "HelloImpl.h"
    #include  <iostream>
    
    using namespace std;
    
    void sayHello () {
        cout << "Hello World!" << endl;
        return;
    }
    

    Hello.c (I prefer to compile jni in c):

    #include <jni.h>
    #include "Hello.h"
    #include "HelloImpl.h"
    
    JNIEXPORT void JNICALL Java_Hello_sayHello (JNIEnv *env, jobject obj) {
        sayHello();
        return;
    }
    

    Finally, we can compile them in some steps:

    1. compile obj (generate HelloImpl.o)

    g++ -c -I”/opt/java/include”
    -I”/opt/java/include/linux” HelloImpl.cpp

    1. compile jni with .o

    g++ -I”/opt/java/include”
    -I”/opt/java/include/linux” -o libhello.so -shared
    -Wl,-soname,hello.so Hello.c HelloImpl.o -static -lc

    in step 2, we use g++ to compile it. This is very important. yor can see How to mix C and C++

    After compilation, you can check the function naming with nm:

    $ nm libhello.so |grep say
    00000708 T Java_Hello_sayHello
    00000784 t _GLOBAL__I_sayHello
    00000718 T sayHello
    

    There is a Java_Hello_sayHello marked T. It should extactly equal to your native method name. If everything is ok. you can run it:

    $ java -Djava.library.path=. Hello
    Hello World!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
In order to apply a triggered animation to all ToolTip s in my app,
I want use html5's new tag to play a wav file (currently only supported
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.