Click here to Skip to main content
Licence CPOL
First Posted 19 May 2003
Views 100,378
Downloads 3,937
Bookmarked 36 times

Sample JNI Application

By | 19 May 2003 | Article
Sample JNI DLL and Java test code

Introduction

I always wanted to play around with JNI. This is my first attempt so I created a utility DLL called TestImpl.dll that exports several useful functions which are difficult or impossible to implement in JAVA.

Test.java is the sample application which calls each of the functions below.

TestImp.cpp contains the code to produce TestImp.dll and exports the functions below. TestImpl.dll is loaded by Java code using the following line: static {System.loadLibrary("TestImp");}.

public native void print(String msg); 

This is trivial and just prints the supplied string using a printf() statement.

public native byte[] readFile(String path);

This function reads a file and returns a byte array.

public native int searchFile(String path, byte[] b, int bLength);

This function performs a very fast binary search of a file for the supplied byte array.

public native String[] findFiles(String path, String mask); 

This function will return a String[] of file names that match the supplied mask. i.e. *.*, *.java, *.class ...

public native String[] checkProcess(String processName, boolean killIt, 
    boolean printOutput); 

This function will check for a running process (use the same process name you see in Task Manager), list all running processes (use " " for the process name), or kill a process.

public native int startProcess(String commandLine, String workingDir); 

This function will call createProcess().

public native boolean waitForFileMask(String directory, String fileMask); 

This function will wait(block) until a file that matches the supplied mask (*.java, *.class, *.txt ...) appears in the supplied directory.

public native boolean waitForAnyFile(String directory); 

This function will wait(block) until any file is added, renamed or deleted from the supplied directory.

I have included a project (VC++ 6.0) with all required files to build the DLL and a sample Java file that calls each of the above functions. You may need to edit the javac_.bat and Test_run.bat files to point to your Java installation (I run multiple versions).

Enjoy!

History

  • 20th May, 2003: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rod VanAmburgh

Web Developer

United States United States

Member

I enjoy working with C#, Java, Perl and C++

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionJava callback PinmemberMember 397024020:39 11 Jul '11  
AnswerRe: Java callback PinmemberRod VanAmburgh8:57 12 Jul '11  
GeneralGood, but less of unicode support Pinmemberst_shadow6:02 24 Feb '09  
Generalhii Pinmembert-819:26 29 Jul '08  
Questionwhat if i want to have my own file names in native code? PinmemberReza Bakhshi19:21 28 Jun '08  
GeneralGood sample Pinmembernickong22:37 20 Apr '08  
QuestionHow to call a DLL developed in C++ without re-Implimentation? Pinmemberyasirqau19:57 9 Apr '08  
AnswerRe: How to call a DLL developed in C++ without re-Implimentation? PinmemberReza Bakhshi19:22 28 Jun '08  
GeneralRe: How to call a DLL developed in C++ without re-Implimentation? Pinmemberyasirqau18:45 29 Jun '08  
Generalnetwork traffic sniffer in Java PinmemberAbhishek Tiwari22:40 8 Apr '05  
GeneralRe: network traffic sniffer in Java PinmemberRod VanAmburgh14:50 9 Apr '05  
GeneralRe: network traffic sniffer in Java Pinmemberslasher1234:32 23 Feb '06  
GeneralThanks a lot.. Pinmemberjay2419:17 17 Oct '04  
GeneralRe: Thanks a lot.. Pinmembersaiusha2:59 23 Nov '04  
GeneralHi Pinmemberwei leong22:46 11 Oct '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 20 May 2003
Article Copyright 2003 by Rod VanAmburgh
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid