Skip to content

Commit 7496b5e

Browse files
Add introduction and Library Mode reference to "Run a Java Library" (#293)
1 parent 81ac93e commit 7496b5e

File tree

1 file changed

+65
-15
lines changed

1 file changed

+65
-15
lines changed

sites/cheerpj/src/content/docs/10-getting-started/02-Java-library.md

Lines changed: 65 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,82 @@ title: Run a Java library
33
description: Use Java classes in JavaScript
44
---
55

6-
## 1. Include CheerpJ on your page
6+
CheerpJ can load and run Java libraries directly in the browser, allowing you to call Java classes and methods from JavaScript with no modifications to your original code. This page will help you get started with CheerpJ and show you how to use an existing Java library (`.jar` file) within a web application.
77

8-
```html
9-
<script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script>
8+
Java source code is not required to use CheerpJ. If you are using your own library, you should already have its compiled `.jar` file available.
9+
10+
**To get started you will need:**
11+
12+
- Your `.jar` file.
13+
- An HTML file where your Java app will be wrapped.
14+
- A simple HTTP server to test your webpage locally.
15+
16+
## 1. Create a Project Directory
17+
18+
Let's start by creating a project folder where all your files will be. Copy your java and future HTML files here.
19+
20+
```shell
21+
mkdir directory_name
1022
```
1123

12-
## 2. Initialize CheerpJ and load your Java library
24+
## 2. Create a Basic HTML File
25+
26+
Let's create a basic HTML file and include and initialize CheerpJ on your page. The `cheerpjInit` command initialises the CheerpJ runtime environment.
1327

14-
```js
15-
await cheerpjInit();
16-
const cj = await cheerpjRunLibrary("/app/library.jar");
28+
```html title="index.html" {6, 10}
29+
<!doctype html>
30+
<html lang="en">
31+
<head>
32+
<meta charset="utf-8" />
33+
<title>CheerpJ Library Mode Test</title>
34+
<script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script>
35+
</head>
36+
<body>
37+
<script>
38+
await cheerpjInit();
39+
</script>
40+
</body>
41+
</html>
1742
```
1843

19-
> [!help] Don't forget to use the /app/ prefix
20-
> It is common for first-time users to forget to add the prefix “/app/” when passing the JAR location to cheerpJRunLibrary().
44+
## 3. Load and Call the Java Library From Java
2145

22-
This will load `library.jar` from the root of your web server.
46+
Now we can load your Java library by calling `cheerpjRunLibrary` which will load the library from the root of your web server. We are assuming your HTML file and your `.jar` files are under the project directory you just created.
2347

24-
## 3. Call Java from JavaScript
48+
```html title="index.html" {12-15}
49+
<!doctype html>
50+
<html lang="en">
51+
<head>
52+
<meta charset="utf-8" />
53+
<title>CheerpJ Library Mode Test</title>
54+
<script src="https://cjrtnc.leaningtech.com/4.2/loader.js"></script>
55+
</head>
56+
<body>
57+
<script>
58+
await cheerpjInit();
59+
// Example classes and methods — replace these with those from your own library
60+
const cj = await cheerpjRunLibrary("/app/library.jar");
61+
const MyClass = await cj.com.library.MyClass;
62+
const obj = await new MyClass();
63+
await obj.myMethod();
64+
</script>
65+
</body>
66+
</html>
67+
```
68+
69+
## 3. Host your page
70+
71+
You can now serve this web page on a simple HTTP server, such as the http-server utility.
2572

26-
```js
27-
const MyClass = await cj.com.library.MyClass;
28-
const obj = await new MyClass();
29-
await obj.myMethod();
73+
```shell
74+
npx http-server -p 8080
3075
```
3176

77+
## The Result
78+
79+
You will see CheerpJ initialize in your browser and load the Java library. Once loaded, the methods from your library can be called directly from JavaScript, just as in the example above. Depending on the size of your library and the optimizations applied, this may take just a few seconds before your code begins executing.
80+
3281
## Further reading
3382

83+
- [Learn more about Library Mode](/docs/guides/library-mode)
3484
- [`cheerpjRunLibrary` reference](/docs/reference/cheerpjRunLibrary)

0 commit comments

Comments
 (0)