Solved

JNI and return a boolean?

Posted on 2000-04-20
4
3,895 Views
Last Modified: 2013-11-23
Can anyone show me a sample on how to return a boolean
here is my header.

JNIEXPORT jboolean JNICALL Java_JProfileIO_Open
  (JNIEnv *, jobject, jint, jstring);

not quite sure on how to return a boolena.

Thanks
0
Comment
Question by:eric07
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
falter earned 10 total points
ID: 2736710
Hi eric07,

simply like that:

JNIEXPORT jboolean JNICALL Java_JProfileIO_Open
                      (JNIEnv *, jobject, jint, jstring) {
return (bool) true;
}

Have a look at the JNI part of the JavaTutorial.

I think every integral datatype like boolean, int , long ...
can be returned by using the corresponding datatype in C language.
Means
java boolean equals C bool
and so on.

Its a little bit more difficult to return an object, but also possible.

If the above code fragment doesn't work for you, post your code.

regards
jf
0
 

Author Comment

by:eric07
ID: 2736899
Thanks
0
 

Author Comment

by:eric07
ID: 2736909
I tried the following but I get this error when I run the app:

I create a java package:

package jifwini;


public class JProfileIO
{
    static
      {
            // Used to load the library
        System.loadLibrary("jifwini");
    }
   
      public JProfileIO(){}
      public native boolean T();
}

then i compile it as following then
create a jar file

source for making the jar file:

cd c:\toolkit\jifwini\jar
del jifwini.jar


jar cf c:\toolkit\jifwini\jar\jifwini.jar -C c:\toolkit\jifwini\class jifwini\JProfileIO.class

then I create a javah file:

del c:\toolkit\jifwini\win32dll\JProfileIO.h

javah -jni -d "c:\toolkit\jifwini\win32dll" -classpath "c:\toolkit\jifwini\class\jifwini" JProfileIO

then i create a dll.  
I created a directory in my c:\temp\jio
where I place the dll.

My java app that uses the jni is as follows:

import jifwini.*;

class Test
{
      public static Test obT;
      String strValue;
      
      public Test()
      {
            Test1();
      }
        
      public void Test1()
      {
            strValue = "Test this one";
            System.out.println( strValue );            

            JProfileIO io = new JProfileIO();
            if( io.T() == true )
                  System.out.println( "true" );
            else
                  System.out.println( "false" );

      }
      
      public static void main(String[] args)
      {
            Test objT = new Test();
      }
      
}


Then i compile and run it:

javac -classpath "c:\temp\jio\jifwini.jar;c:\temp\jio" *.java
java -classpath "c:\temp\jio\jifwini.jar;c:\temp\jio" Test


now it compiles fine but when it runs I get the following erro:



Exception in thread "main" java.lang.UnsatisfiedLinkError: T
at jifwini.JPRofileIO.T(Native Method)
at Test.Test1(Test.java:19)
at Test.<init>(Test.java:10)
at Test.main(Test.java:31)

Here is what the dll does:

JNIEXPORT jboolean JNICALL Java_JProfileIO_T
  (JNIEnv *env, jobject this)
{
      return (JNI_TRUE);
}

0
 
LVL 3

Expert Comment

by:falter
ID: 2737466
eric07,

first your DLL is named jifwini.dll ?
System.loadLibrary is extending the given parameter to an operating system(OS) dependend shared library name.
Win: jfiwini.dll
Solaris: libjfwinwi.sl and so on.

Second the OS must be able to find the library.
Assuming you are using Win95/98 try to place the library in a directory where your PATH environment points to.
- for example:
do echo %PATH% at a command prompt and
you will get a list of directories where you can put it.

third there is another load call you can use:
System.load("c:\\temp\\jio\\fiwini.dll");
I never tried this because this will only run at one platform.

Happy eastern
jf
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now