Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Sample JNI Application

0.00/5 (No votes)
19 May 2003 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here