Simplified java binding example

This commit is contained in:
Joseph Henry
2017-10-16 13:10:20 -07:00
parent 1a4ad49e06
commit 5de02d3259
13 changed files with 111 additions and 55 deletions

2
.gitignore vendored
View File

@@ -1,6 +1,8 @@
# Binaries/Executables # Binaries/Executables
*.dll *.dll
*.lib *.lib
*.so
*.dylib
*.o *.o
*.class *.class

View File

@@ -306,6 +306,7 @@ utilities:
$(CXX) $(CXXFLAGS) -c src/Utilities.cpp \ $(CXX) $(CXXFLAGS) -c src/Utilities.cpp \
$(ZT_DEFS) $(ZT_INCLUDES) $(LIBZT_INCLUDES) $(STACK_DRIVER_DEFS) $(ZT_DEFS) $(ZT_INCLUDES) $(LIBZT_INCLUDES) $(STACK_DRIVER_DEFS)
# windows DLL
win_dll: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS) win_dll: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS)
# First we use mingw to build our DLL # First we use mingw to build our DLL
@mkdir -p $(BUILD) obj @mkdir -p $(BUILD) obj
@@ -317,16 +318,19 @@ win_dll: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS)
# lib /machine:x64 /def:libzt.def # lib /machine:x64 /def:libzt.def
# or just execute: makelib # or just execute: makelib
# ordinary shared library
shared_lib: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS) shared_lib: lwip lwip_driver libzt_socket_layer utilities $(ZTO_OBJS)
@mkdir -p $(BUILD) obj @mkdir -p $(BUILD) obj
mv *.o obj mv *.o obj
$(CXX) $(CXXFLAGS) -shared -o $(BUILD)/libzt.so obj/*.o $(CXX) $(CXXFLAGS) -shared -o $(BUILD)/libzt.so obj/*.o
# dynamic library for use with Java JNI, scala, etc
shared_jni_lib: lwip lwip_driver libzt_socket_layer jni_socket_wrapper utilities $(ZTO_OBJS) shared_jni_lib: lwip lwip_driver libzt_socket_layer jni_socket_wrapper utilities $(ZTO_OBJS)
@mkdir -p $(BUILD) obj @mkdir -p $(BUILD) obj
mv *.o obj mv *.o obj
$(CXX) $(CXXFLAGS) -shared -o $(BUILD)/libzt.so obj/*.o $(CXX) $(CXXFLAGS) -dynamiclib -o $(BUILD)/libzt.dylib obj/*.o
# static library
ifeq ($(STACK_PICO),1) ifeq ($(STACK_PICO),1)
static_lib: picotcp picotcp_driver libzt_socket_layer utilities $(ZTO_OBJS) static_lib: picotcp picotcp_driver libzt_socket_layer utilities $(ZTO_OBJS)
@mkdir -p $(BUILD) obj @mkdir -p $(BUILD) obj

View File

@@ -28,13 +28,13 @@
import zerotier.*; import zerotier.*;
public class MyClass { public class ExampleApp {
public native int loadsymbols(); public native int loadsymbols();
public native void startOneService(); public native void startOneService();
static { static {
System.load("/Users/joseph/op/zt/libzt/build/darwin/libzt.so"); System.loadLibrary("zt");
} }
public static void main(String[] args) { public static void main(String[] args) {

View File

@@ -0,0 +1,18 @@
OSTYPE=$(shell uname -s | tr '[A-Z]' '[a-z]')
BUILD=build/$(OSTYPE)
ifeq ($(OSTYPE),darwin)
SHARED_LIB=libzt.dylib
endif
ifeq ($(OSTYPE),linux)
SHARED_LIB=libzt.so
endif
example_java_app:
javac *.java
copy_dynamic_lib:
cp ../../../$(BUILD)/$(SHARED_LIB) .
clean:
-find . -type f \( -name '*.class' \) -delete

View File

@@ -1,9 +1,14 @@
Java Examples ## ZeroTier with Java via JNI
====== ***
`make shared_jni_lib SDK_JNI=1 SDK_IPV4=1` To get this example project to work, do the following:
Uses API described in [api/java/README.md] - From libzt main directory, build shared library: `make shared_jni_lib`
- Copy `src/ZeroTier.java` into `your_project/src/zerotier` - Copy the resultant dynamic library (`*.so` or `*.dylib`) from `build/` to this current directory
- Copy `src/Address.java` into `your_project/src/zerotier` - Change to this directory and `make example_java_app`
- Copy `libzt.jnilib` or `libzt.so` from `build/` into `your_project/lib` - Run: `java -cp "." ExampleApp`
Notes:
Upon execution, it will load the libzt dynamic library via the `loadLibrary` method and begin generating an identity.

View File

@@ -1,15 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="/Users/joseph/op/zt/libzt"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="/Users/joseph/op/zt/libzt/examples/java/ZeroTierHelloWorld">
<attributes>
<attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="."/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ZeroTierHelloWorld</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@@ -1,11 +0,0 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.8
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.8

View File

@@ -1,2 +0,0 @@
ZeroTierSDK Java API
======

View File

@@ -24,6 +24,8 @@
* of your own application. * of your own application.
*/ */
package zerotier;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;

View File

@@ -24,6 +24,10 @@
* of your own application. * of your own application.
*/ */
package zerotier;
//import zerotier.*;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;

View File

@@ -0,0 +1,7 @@
package zerotier;
public class ZeroTier
{
public native int ztjni_socket(int family, int type, int protocol);
public int socket(int family, int type, int protocol) { return ztjni_socket(family, type, protocol); }
}

View File

@@ -0,0 +1,59 @@
/*
* ZeroTier SDK - Network Virtualization Everywhere
* Copyright (C) 2011-2017 ZeroTier, Inc. https://www.zerotier.com/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* --
*
* You can be released from the requirements of the license by purchasing
* a commercial license. Buying such a license is mandatory as soon as you
* develop commercial closed-source software that incorporates or links
* directly against ZeroTier software without disclosing the source code
* of your own application.
*/
// Simple Java example for libzt using JNI
import zerotier.ZeroTier;
public class example {
public native int loadsymbols();
public native void startOneService();
static {
System.load("/Users/joseph/op/zt/libzt/build/darwin/libzt.so");
}
public static void main(String[] args) {
final ZeroTier z = new ZeroTier();
new Thread(new Runnable() {
public void run() {
System.out.println("starting libzt");
z.ztjni_socket(2, 1, 0);
// start(path) will not block
// startjoin(path, nwid) will block
}
}).start();
while(true)
{
try { Thread.sleep(3000); }
catch (InterruptedException e) { e.printStackTrace(); }
}
}
}