Solved

how to get javah to work with a package?

Posted on 1998-05-27
3
462 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 50 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

791 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