Solved

JNI and return a boolean?

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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 make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

717 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