Click here to Skip to main content
12,445,025 members (53,728 online)
Click here to Skip to main content
Add your own
alternative version

Stats

120.6K views
5.1K downloads
37 bookmarked
Posted

Sample JNI Application

, 19 May 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

Rod VanAmburgh
Web Developer
United States United States
I enjoy working with C#, Java, Perl and C++

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionview contents of ITLSSPProc.dll Pin
Member 977863922-Jan-13 19:55
memberMember 977863922-Jan-13 19:55 
QuestionJava callback Pin
Member 397024011-Jul-11 20:39
memberMember 397024011-Jul-11 20:39 
AnswerRe: Java callback Pin
Rod VanAmburgh12-Jul-11 8:57
memberRod VanAmburgh12-Jul-11 8:57 
GeneralGood, but less of unicode support Pin
st_shadow24-Feb-09 6:02
memberst_shadow24-Feb-09 6:02 
Generalhii Pin
t-829-Jul-08 19:26
membert-829-Jul-08 19:26 
Questionwhat if i want to have my own file names in native code? Pin
Reza Bakhshi28-Jun-08 19:21
memberReza Bakhshi28-Jun-08 19:21 
GeneralGood sample Pin
nickong20-Apr-08 22:37
membernickong20-Apr-08 22:37 
QuestionHow to call a DLL developed in C++ without re-Implimentation? Pin
yasirqau9-Apr-08 19:57
memberyasirqau9-Apr-08 19:57 
AnswerRe: How to call a DLL developed in C++ without re-Implimentation? Pin
Reza Bakhshi28-Jun-08 19:22
memberReza Bakhshi28-Jun-08 19:22 
GeneralRe: How to call a DLL developed in C++ without re-Implimentation? Pin
yasirqau29-Jun-08 18:45
memberyasirqau29-Jun-08 18:45 
Generalnetwork traffic sniffer in Java Pin
Abhishek Tiwari8-Apr-05 22:40
memberAbhishek Tiwari8-Apr-05 22:40 
GeneralRe: network traffic sniffer in Java Pin
Rod VanAmburgh9-Apr-05 14:50
memberRod VanAmburgh9-Apr-05 14:50 
GeneralRe: network traffic sniffer in Java Pin
slasher12323-Feb-06 4:32
memberslasher12323-Feb-06 4:32 
GeneralThanks a lot.. Pin
jay2417-Oct-04 19:17
memberjay2417-Oct-04 19:17 
GeneralRe: Thanks a lot.. Pin
saiusha23-Nov-04 2:59
membersaiusha23-Nov-04 2:59 
GeneralHi Pin
wei leong11-Oct-04 22:46
memberwei leong11-Oct-04 22:46 

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

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

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