13 changed files with 111 additions and 55 deletions
@ -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
|
||||||
@ -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. |
||||||
@ -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> |
|
||||||
@ -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> |
|
||||||
@ -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 |
|
||||||
@ -1,2 +0,0 @@ |
|||||||
ZeroTierSDK Java API |
|
||||||
====== |
|
||||||
@ -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); } |
||||||
|
} |
||||||
@ -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(); } |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
Loading…
Reference in new issue