Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Exception handling in JNI

, 7 Feb 2007
An article on Exception handling in JNI
jni_ex_src.zip
c++
java
.classpath
.project
org
tutorial
jni
util
exception
JniException.class
TestJniException.class
/**
 * 
 */
package org.tutorial.jni.util.exception;

import java.io.File;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;

/**
 * @author jafarmlp@googlemail.com
 * 
 */
public class TestJniException {

	/**
	 * This is an example for JNI_ASSERT
	 *
	 */
	public native void testexception();

	/**
	 * This is an example of try/catch
	 *
	 */
	public native void function1();

	/**
	 * This loads the native library
	 */
	
	static {
		try {
			
			//System.loadLibrary("JniException");
			System.load(new File(".").getCanonicalPath()
					+ "\\..\\c++\\Debug\\JniException.dll");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 */
	public TestJniException() {
		super();
	}
	/**
	 * This is a SWT MessageBox.
	 * @param e
	 */
	public void MsgBox(Exception e) {
		//Get the stack trace and the details from c++
		StackTraceElement st[] = e.getStackTrace();
		StringBuffer errorMessage = new StringBuffer(e.getMessage()
				+ "\n\nJava StackTrace :\n\n");
		for (int i = 0; i < st.length; i++)
			errorMessage.append(st[i].toString() + "\n");
		Display display = new Display();
		Shell shell = new Shell(display);
		MessageBox messageBox = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
		messageBox.setText("JNI Exception !");
		messageBox.setMessage(errorMessage.toString());
		messageBox.open();
	}

	/**
	 * Main method.
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("JNI Exception example");
		TestJniException testJniException = new TestJniException();
		try {
			testJniException.testexception();
		} catch (Exception e) {
			testJniException.MsgBox(e);
		}
		testJniException.function1();
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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

About the Author

jafarmlp
Software Developer (Senior)
India India
Jafar is working in software field for the last five years. He is from Moonniyoor, a village of north Kerala,India.
Visit Jafar's home page http://jafarmlp.googlepages.com/

| Advertise | Privacy | Mobile
Web03 | 2.8.140709.1 | Last Updated 8 Feb 2007
Article Copyright 2007 by jafarmlp
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid