Solved

JNI and return a boolean?

Posted on 2000-04-20
4
4,218 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
[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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.

762 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