Skip to content

Commit

Permalink
improved 'getting started' by separating extensions content and point…
Browse files Browse the repository at this point in the history
…ing to the docs
  • Loading branch information
theodoravraimakis committed Dec 2, 2024
1 parent e536b7c commit 5032d8c
Show file tree
Hide file tree
Showing 7 changed files with 1,553 additions and 43 deletions.
467 changes: 467 additions & 0 deletions packages/astro-theme/components/AppletRunner.astro

Large diffs are not rendered by default.

912 changes: 912 additions & 0 deletions packages/astro-theme/components/JNLPRunner.astro

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions packages/astro-theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {},
Expand Down
20 changes: 11 additions & 9 deletions sites/cheerpj/src/content/docs/00-overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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";

<div class="not-prose flex gap-2 mb-2">
<img
Expand Down Expand Up @@ -40,17 +42,17 @@ import { DISCORD_URL } from "@/consts.ts";
repository](https://github.com/leaningtech/cheerpj-meta)
</div>

```html
<script src="https://cjrtnc.leaningtech.com/3.0/cj3loader.js"></script>
```
<div class="mx-24">![](./explanation-diagram.png)</div>

<div class="text-stone-400">
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.
</div>
### **Not a developer?**

<div class="mx-24">![](./explanation-diagram.png)</div>
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. */}

<div class="items-center justify-center space-y-4 sm:flex sm:space-y-0 sm:space-x-4 rtl:space-x-reverse">
<AppletRunner />
<JNLPRunner />
</div>

## Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
{/* <p class="m-0 text-md font-semibold text-gray-900 dark:text-white"> */}

- [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 `<applet>` on public websites.
<div class="items-center justify-center space-y-4 sm:flex sm:space-y-0 sm:space-x-4 rtl:space-x-reverse">
<div class="-mb-2 flex-grow text-left">
<div class="-my-1 font-sans text-lg font-semibold">Not a Developer?</div>
<p class="-my-3 text-base text-gray-600 dark:text-gray-400">
Check out our browser extension for running Java Applets directly in
modern browsers.
</p>
</div>
<AppletRunner />
</div>

## Running your own applet

Expand Down Expand Up @@ -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. `<cheerpj-applet>`, `<cheerpj-object>`, and `<cheerpj-embed>` are all supported.
## Running a public applet

### 1. Install the CheerpJ applet runner

CheerpJ Applet Runner is available for Chrome and Edge.

<div class="flex flex-wrap gap-3">
<LinkButton
type="primary"
href="https://chrome.google.com/webstore/detail/cheerpj-applet-runner/bbmolahhldcbngedljfadjlognfaaein"
label="Add to Chrome"
iconLeft="openmoji:chrome"
/>

<LinkButton
type="primary"
href="https://microsoftedge.microsoft.com/addons/detail/cheerpj-applet-runner/ebfcpaoldmijengghefpohddmfpndmic"
label="Add to Microsoft Edge"
iconLeft="openmoji:edge"
/>

</div>

### 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<div class="items-center justify-center space-y-4 sm:flex sm:space-y-0 sm:space-x-4 rtl:space-x-reverse">
<div class="flex-grow text-left ">
<div class="-my-1 font-sans text-lg font-semibold">Not a Developer?</div>
<p class="-my-3 text-base text-gray-600 dark:text-gray-400">
Check out our browser extension for running Java Web Start applications
directly in modern browsers.
</p>
</div>
<JNLPRunner />
</div>

You will need:

Expand Down
Original file line number Diff line number Diff line change
@@ -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_<fully-qualified-class-name>_<method-name>`. 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_<fully-qualified-class-name>_<method-name>(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_<fully-qualified-class-name>_<method-name>`.

```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}
<!doctype html>
<html>
<head>
<title>Native Method Example</title>
<script src="https://cjrtnc.leaningtech.com/3.0/cj3loader.js"></script>
</head>
<body>
<script>
async function Java_Example_alert(lib, str) {
window.alert(str);
}
await cheerpjInit({ natives: { Java_Example_alert } });
await cheerpjRunMain("Example", "/app/");
</script>
</body>
</html>
```

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

0 comments on commit 5032d8c

Please sign in to comment.