diff --git a/packages/astro-theme/components/AppletRunner.astro b/packages/astro-theme/components/AppletRunner.astro new file mode 100644 index 00000000..acc532f5 --- /dev/null +++ b/packages/astro-theme/components/AppletRunner.astro @@ -0,0 +1,467 @@ + + +
+
Run Java Applets in your browser.
+
+ CheerpJ Applet Runner +
+
+
diff --git a/packages/astro-theme/components/JNLPRunner.astro b/packages/astro-theme/components/JNLPRunner.astro new file mode 100644 index 00000000..0470b0c9 --- /dev/null +++ b/packages/astro-theme/components/JNLPRunner.astro @@ -0,0 +1,912 @@ + + +
+
Run Java Web Start apps in your browser.
+
JNLP Runner
+
+
diff --git a/packages/astro-theme/package.json b/packages/astro-theme/package.json index a8be6d4b..b5e5d96d 100644 --- a/packages/astro-theme/package.json +++ b/packages/astro-theme/package.json @@ -23,6 +23,8 @@ "./components/LatestBlogPostPill.astro": "./components/LatestBlogPostPill.astro", "./components/ShowcaseList.astro": "./components/ShowcaseList.astro", "./components/Callout.astro": "./components/Callout.astro", + "./components/JNLPRunner.astro": "./components/JNLPRunner.astro", + "./components/AppletRunner.astro": "./components/AppletRunner.astro", "./components/nav/global/GlobalNavbar.astro": "./components/nav/global/GlobalNavbar.astro" }, "scripts": {}, diff --git a/sites/cheerpj/src/content/docs/00-overview.mdx b/sites/cheerpj/src/content/docs/00-overview.mdx index a77826bb..7ec018b6 100644 --- a/sites/cheerpj/src/content/docs/00-overview.mdx +++ b/sites/cheerpj/src/content/docs/00-overview.mdx @@ -7,6 +7,8 @@ description: Java Virtual Machine for modern web browsers import LinkButton from "@leaningtech/astro-theme/components/LinkButton.astro"; import ShowcaseList from "@leaningtech/astro-theme/components/ShowcaseList.astro"; import { DISCORD_URL } from "@/consts.ts"; +import JNLPRunner from "@leaningtech/astro-theme/components/JNLPRunner.astro"; +import AppletRunner from "@leaningtech/astro-theme/components/AppletRunner.astro";
-```html - -``` +
![](./explanation-diagram.png)
-
- Not a developer? Check out our [product website](https://cheerpj.com/) and our - ready-to-use [browser extension](/cheerpj-jnlp-runner) for running Java Web - Start applications. -
+### **Not a developer?** -
![](./explanation-diagram.png)
+Check out our browser extensions for running Java Applets and Web Start applications. Designed for individual users and organizations, they require no technical knowledge or setup. +{/* Check out our browser extensions for **running Java Applets and Web Start applications directly in modern browsers**, without the need for a Java plugin or a Java installation. */} + +
+ + +
## Features diff --git a/sites/cheerpj/src/content/docs/10-getting-started/01-Java-applet.mdx b/sites/cheerpj/src/content/docs/10-getting-started/01-Java-applet.mdx index 636b3a6d..23d40a25 100644 --- a/sites/cheerpj/src/content/docs/10-getting-started/01-Java-applet.mdx +++ b/sites/cheerpj/src/content/docs/10-getting-started/01-Java-applet.mdx @@ -4,13 +4,22 @@ description: Run a java applet in modern browsers --- import LinkButton from "@leaningtech/astro-theme/components/LinkButton.astro"; +import AppletRunner from "@leaningtech/astro-theme/components/AppletRunner.astro"; -CheerpJ can run Java applets in the browser seamlessly. This page will help you getting started with CheerpJ for Java applets. +CheerpJ can run Java applets in the browser seamlessly. This page will help you getting started with CheerpJ for Java applets using the CheerpJ runtime environment on your own webpage. -**There are two different ways to run a Java Applet in the browser:** +{/*

*/} -- [Running your own Java applet](/docs/getting-started/Java-applet#running-your-own-applet) using the CheerpJ runtime environment in your own webpage. -- [Running a public applet](/docs/getting-started/Java-applet#running-a-public-applet) using the [CheerpJ Applet Runner](https://chrome.google.com/webstore/detail/cheerpj-applet-runner/bbmolahhldcbngedljfadjlognfaaein) Chrome extension for applets integrated with the applet tag `` on public websites. +
+
+
Not a Developer?
+

+ Check out our browser extension for running Java Applets directly in + modern browsers. +

+
+ +
## Running your own applet @@ -75,35 +84,6 @@ http-server -p 8080 > In case your users have a native Java plugin installed, you can replace the original HTML tag with a `cheerpj-` prefixed version. ``, ``, and `` are all supported. -## Running a public applet - -### 1. Install the CheerpJ applet runner - -CheerpJ Applet Runner is available for Chrome and Edge. - -
- - - - -
- -### 2. Go to a website with an applet - -Visit a page with a Java applet, [such as this one](http://schubart.net/rc/) and click on the CheerpJ Applet Runner icon in the toolbar and enable CheerpJ. - -![](/docs/cheerpj3/assets/applet_runner_demo.gif) - ## The result You will see the CheerpJ display on your browser with some loading messages before showing your applet running. Depending on your application and the optimizations applied, this could take just a few seconds. diff --git a/sites/cheerpj/src/content/docs/10-getting-started/03-JNLP.md b/sites/cheerpj/src/content/docs/10-getting-started/03-JNLP.mdx similarity index 93% rename from sites/cheerpj/src/content/docs/10-getting-started/03-JNLP.md rename to sites/cheerpj/src/content/docs/10-getting-started/03-JNLP.mdx index 21899c24..240ecf12 100644 --- a/sites/cheerpj/src/content/docs/10-getting-started/03-JNLP.md +++ b/sites/cheerpj/src/content/docs/10-getting-started/03-JNLP.mdx @@ -3,9 +3,20 @@ title: Run a JNLP description: Run a JWS/JNLP application in the browser --- +import JNLPRunner from "@leaningtech/astro-theme/components/JNLPRunner.astro"; + This quickstart tutorial will take you step by step on how to run your JNLP app (also known as Java Web Start application) in the browser with CheerpJ. -If you are interested in a ready-to-use tool for running Java Web Start applications, we recommend taking a look at our [CheerpJ JNLP Runner](/cheerpj-jnlp-runner) browser extension. +
+
+
Not a Developer?
+

+ Check out our browser extension for running Java Web Start applications + directly in modern browsers. +

+
+ +
You will need: diff --git a/sites/cheerpj/src/content/docs/11-guides/implementing-native-methods.md b/sites/cheerpj/src/content/docs/11-guides/implementing-native-methods.md new file mode 100644 index 00000000..850d0a59 --- /dev/null +++ b/sites/cheerpj/src/content/docs/11-guides/implementing-native-methods.md @@ -0,0 +1,136 @@ +--- +title: Implementing Native Methods +description: Java Native Interface (JNI) with CheerpJ +--- + +CheerpJ allows one to implement Java native methods (typically written in C/C++ or another AOT-compiled language) directly in JavaScript, similar to the Java Native Interface (JNI) in standard Java. + +In Java, native methods are identified by the `native` keyword in their declaration. These methods are not implemented in Java but are instead defined in an external language, which in the case of CheerpJ, is JavaScript. + +## Overview + +In general, we can implement native methods in CheerpJ by following these steps: + +1. Declare a native method in Java using the `native` keyword. +2. Implement the native method in JavaScript. +3. Pass the native function to CheerpJ. + +## Declaring Native Methods in Java + +To declare a native method in Java, use the `native` keyword in the method declaration. Here’s an example of a Java class with a native method: + +```java title="Example.java" +public class Example { + public static void main(String[] args) { + alert("Hello, world!"); + } + + public static native void alert(String message); +} +``` + +The method is defined in the Java class but is not implemented in Java. Instead, the implementation will be provided in JavaScript. + +## Implementing Native Methods in JavaScript + +To implement a native method in JavaScript, create an `async` function that follows the naming convention `Java__`. For instance, if `com.foo.Bar` has a native method called `baz`, its object key is `Java_com_foo_Bar_baz`. The function takes: + +- A [`CJ3Library`] object `lib` as the first parameter (which provides access to other classes and methods within the library). +- `self` as the second parameter, the instance of the Java class calling the native method. This parameter can be omitted for static native methods. +- The native method’s parameters as subsequent parameters. + +And returns a value or a Promise that resolves to a value. The function syntax is as follows: + +```js +async function Java__(lib, self, param1, param2, ...) { + // Implementation +} +``` + +> [!info] Handling Static Native Methods +> If the native method is static, the `self` parameter can be omitted. + +## Passing Native Functions to CheerpJ + +To use the native method in CheerpJ, pass the function to the [`cheerpjInit`] function as a property of the [`natives`] option. You can pass: + +1. **The function definition directly**: + +```js +await cheerpjInit({ + natives: { + async Java_Example_alert(lib, str) { + window.alert(str); + }, + }, +}); +``` + +2. **Or just the function name if it was defined earlier**: + +```js +async function Java_Example_alert(lib, str) { + window.alert(str); +} + +await cheerpjInit({ natives: { Java_Example_alert } }); +``` + +## Converting Parameters and Return Values + +Parameters and return values of JNI calls are automatically converted between JavaScript and Java types based on [`conversion rules`]. + +## Example Walkthrough + +Here’s a full example that demonstrates the native method setup in Java and its JavaScript implementation. + +1. Declare a native method in Java using the `native` keyword: + +```java title="Example.java" +public class Example { + public static void main(String[] args) { + alert("Hello, world!"); + } + + public static native void alert(String message); +} +``` + +2. Implement the native method by creating an `async` function in JavaScript that follows the naming convention `Java__`. + +```js title="index.html" +async function Java_Example_alert(lib, str) { + window.alert(str); +} +``` + +Here, we provide an implementation for the `alert` method in the `Example` class, by creating a function named `Java_Example_alert`. The function displays an alert dialog with the message using `window.alert`. + +3. Initialize CheerpJ with the `natives` option and pass the native method implementation to [`cheerpjInit`]: + +```html title="index.html" {13} + + + + Native Method Example + + + + + + +``` + +In this setup, [`cheerpjInit`] loads `Java_Example_alert` as the native method implementation. When `Example.alert` is called in Java, it triggers the JavaScript `Java_Example_alert` function, displaying an alert dialog with the message. + +[`natives`]: /docs/reference/cheerpjInit#natives +[`CJ3Library`]: /docs/reference/CJ3Library +[`conversion rules`]: /docs/reference/CJ3Library#conversion-rules +[`cheerpjInit`]: /docs/reference/cheerpjInit