Solved

how to get javah to work with a package?

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
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 video teaches viewers about errors in exception handling.

707 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

12 Experts available now in Live!

Get 1:1 Help Now