Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to get javah to work with a package?

Posted on 1998-05-27
3
Medium Priority
?
493 Views
Last Modified: 2012-06-21
I have a file called RS232.java  initially it did not belong to a package so I could simply do a javac RS232.java and then javah -jni RS232.  Now I add the statement package bird; to RS232 and can no longer use javah.  RS232 is in the directory bird and I have tried javah -jni bird.RS232 from 'bird' parent directory which is where I compiled this application.  I also tried javah -jni RS232 from within the bird directory.  No matter what I try I always get the error  'RS232: no such class' The whole application compiles and runs fine bur crashes when a native call is attempted.
0
Comment
Question by:micah
  • 2
3 Comments
 

Author Comment

by:micah
ID: 1221566
Edited text of question
0
 
LVL 4

Accepted Solution

by:
evijay earned 200 total points
ID: 1221567
Let me illustrate the whole process with example



Let me suppose that you are working in a directory say
c:\work
now create a directory in work by name bird
c:\work> md bird
change to directory bird
c:work> cd bird

create a file by name RS232.java with following contents

package bird;
public class RS232 {

      public static native void printIt();
      static {
            System.loadLibrary("rs232");
      }

      public static void main(String args[])
      {
            printIt();
      }
}

compile the file using

c:\work\bird>javac RS232.java

now set the class path to point to c:\work (the parent directory of bird) (this is most important)
c:\work\bird> set classpath=c:\work;%classpath%    

create stub file
c:\work\bird> javah -jni bird.RS232

This creates a file bird_RS232.h. Now,create a file rs232.c (native implementation) in bird directory with the following contents

#include "bird_RS232.h"

JNIEXPORT void JNICALL Java_bird_RS232_printIt
  (JNIEnv *env, jclass mclass)
{
      printf("Hello World !!\n");

}

compile the file using VC++ compiler as follows

C:\work\bird>cl -Ic:\jdk1.1.4\include -Ic:\jdk1.1.4\include\win32  -LD rs232.c -Fers232.dll c:\jdk1.1.4\lib\javai.lib

(change the jdk directory to appropriate to your installation. I am assuming that my jdk is in jdk1.1.4)

Now, run the program as
c:\work\bird> java bird.RS232

I did it in my system and it worked fine. Hope that it will work in your system too !!



0
 
LVL 4

Expert Comment

by:evijay
ID: 1221568
Another site for interesting info on native methods

http://www.inside-java.com/articles/native/index.htm
0

Featured Post

Technology Partners: 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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

877 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