Solved

how to get javah to work with a package?

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

Industry Leaders: 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

Suggested Solutions

Title # Comments Views Activity
fibonacci ten numbers 4 54
Why my table column Id is not passed to java object? 4 46
Notify sent to other threads in Java 9 42
Coding for the first time 9 66
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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:

756 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