Creating a Java example
Java example using Eclipse
This page describes the procedure to build a Java example using the JNI interface included in the SDK, Core, and Linux Editions since Open NFC 4.3.0. The Java API of Open NFC is described in SIS_NFC_1003-188 document included in the release.
Source files
All files related to this procedure are located under the folder core/porting/jni. This folder contains:
- output/
- open_nfc_client_jni{,64}.dll :
- The specific JNI client libraries to use with the appropriate server (e.g., server_porting.exe for Win32)
- open_nfc.jar :
- This contains the Open NFC framework, to link with your Java project.
- example1.jar, example1.bat :
- This is a sample of the command-line to use to run the example.
- example1/example/:
- *.java
- This are sample applications that can be used to better understand the usage of the API. Each file is a separate complete example.
Creating a new project
This section assumes that you are familiar with the client/server flavour of Open NFC — the JNI wrapper is only available in this flavour at the moment. If not, please try using first the win32_examples or Linux examples to learn how to start all components: Connection Center, Simulator or board, Open NFC Server, and finally client application.
We are also assuming that you have downloaded and unzipped the archive of the Open NFC SDK Edition (you could use as well the Core or Linux Edition), and that the files are available under /path/to/core/porting/jni.
We are giving these explanations for the Eclipse IDE, but of course it is perfectly possible to use a different development environment.
- Create a new project via “File” -> “New” -> “Java Project”
- Choose the name you want, we use “JNIExample” here.
- Click “Next >” then go to the “Libraries” tab.
- Click “Add External JARs…” and select the file /path/to/core/porting/jni/output/open_nfc.jar
, then finally click “Finish”. The new project is created.
- Under “JNIExample” > “src” create a new Class, for example “JNIExampleClass”
- Create the code you want, importing org.opennfc.* classes. We assume here that you copy the contents of /path/to/core/porting/jni/example1/example/Example.java, just changing the class name. Of course, you can write any code you like here.
- Open the “(default package)” properties.
- In “Run/Debug Settings”, click “New…” then select “Java Application”.
- Choose the configuration name (“default_run”)
- In the “(x)= Arguments” tab, add the following line in the “VM arguments:” box. Please change the bold values with the appropriate values for your system. The opennfc.config option corresponds to the port your server is listening for clients on.
-Xbootclasspath/p:open_nfc.jar -Dopennfc.config=1234 -Dsun.boot.library.path=. -Djava.library.path=/path/to/core/porting/jni/output/
Running the project
Now in order to run your project, you have to start the following components:
- Connection Center
- NFC Simulator (or start your NFC evaluation kit in the Connection Center)
- Server. Under Win32 environment, it can be found under /path/to/core/porting/win32/win32_examples/release/. Adapt the path for Linux or 64bits version. Start the server with (change nfcc_hal with nfcc_i2c if you have a kit):
server_porting.exe cc:nfcc_hal 1234
Finally, you should be able to run your project from Eclipse, and see an output similar to this:
4.3
Virtual NFCC firmware 3.45a
Back to top 
Except as noted otherwise, this content is licensed under the terms of the
Apache 2.0 License.
For details and restrictions, see the
Content License.
Open NFC is a registered trademark of
Inside Secure, All logos and graphics are copyrighted.