Solved

JNI and return a boolean?

Posted on 2000-04-20
4
3,954 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 76
split53 challenge 7 78
HashMap Vs TreeMap 12 55
github account with ecipse 1 42
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

911 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

20 Experts available now in Live!

Get 1:1 Help Now