Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.AlexTrotsenko:v8-adapter:v8-adapter-1.30'
}
<dependency>
<groupId>com.github.AlexTrotsenko</groupId>
<artifactId>v8-adapter</artifactId>
<version>v8-adapter-1.30</version>
</dependency>
libraryDependencies += "com.github.AlexTrotsenko" % "v8-adapter" % "v8-adapter-1.30"
:dependencies [[com.github.AlexTrotsenko/v8-adapter "v8-adapter-1.30"]]
This project is an add-on for the excellent J2V8 Project, allowing users to easily share Java classes and objects with a V8 runtime.
As this project is built on top of the J2V8 project, you will need to include two dependencies in your pom.xml:
j2v8_macosx_x86_64
or j2v8_win32_x86_64
).An example <dependencies>
section for a project that uses J2V8 on Linux would look like this:
<dependencies>
<!-- J2V8 Adapter -->
<dependency>
<groupId>io.alicorn.v8</groupId>
<artifactId>v8-adapter</artifactId>
<version>1.59</version>
</dependency>
<!-- J2V8 Runtime -->
<dependency>
<groupId>com.eclipsesource.j2v8</groupId>
<artifactId>j2v8_linux_x86_64</artifactId>
<version>4.5.0</version>
</dependency>
</dependencies>
An example dependencies
section for a project that uses J2V8 on Android would look like this:
dependencies {
// J2V8 Adapter
compile 'io.alicorn.v8:v8-adapter:1.41'
// J2V8 Runtime
compile 'com.eclipsesource.j2v8:j2v8:4.8.0@aar'
}
Once you have the adapter included, the only class you need to use is the V8JavaAdapter
class. This class enables you to inject Java objects and classes into the V8 runtime.
Below is a brief example of injecting the Java BitSet class:
public static void main(String[] args) {
// Create the V8 runtime.
V8 v8 = V8.createV8Runtime();
// Inject the Bit Set class into the V8 runtime.
V8JavaAdapter.injectClass(BitSet.class, v8);
// Create a Bit Set in JS and manipulate it.
v8.executeVoidScript("var b = new BitSet(8);");
v8.executeVoidScript("b.set(1);");
v8.executeVoidScript("b.set(3);");
v8.executeVoidScript("b.set(5);");
// Prints out "{1, 3, 5}".
System.out.println(v8.executeStringScript("b.toString();"));
}
Below is an example of injecting an instance of the Java BitSet class into J2V8:
public static void main(String[] args) {
// Create the V8 runtime.
V8 v8 = V8.createV8Runtime();
// Create a Bit Set in Java and manipulate it.
BitSet javaBitSet = new BitSet(8);
javaBitSet.set(0);
javaBitSet.set(2);
javaBitSet.set(4);
// Inject the Bit Set instance into the V8 runtime.
// The first parameter "b" is the name we want the
// injected object to have in V8.
V8JavaAdapter.injectObject("b", javaBitSet, v8);
// Prints out "{0, 2, 4}".
System.out.println(v8.executeStringScript("b.toString();"));
}
This project is production-ready and is already in use in the Alicorn framework. However, there are still features to be added and optimizations to be made! Below is a list that contains some of the things we are either planning to add or in the process of adding (feel free to suggest additions to this list or make pull requests that address items on this list):
This project is licensed under the BSD 3-Clause License, chosen for its inherent patent and trademark protection as well as its clear language. If enough/any issues are filed regarding this license choice (a departure from Apache V2 or the Eclipse Public License used by the J2V8 project), we will change the license to suit popular opinion.
Please feel free to contribute any pull requests, feature requests, or issues that you notice to this project; we want it to be the best it can be!