?
Solved

how to get javah to work with a package?

Posted on 1998-05-27
3
Medium Priority
?
502 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

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

590 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