Skip to content

Commit a483368

Browse files
committed
Add Build Native Image with JS Embedded demo
1 parent 237f72d commit a483368

File tree

6 files changed

+184
-1
lines changed

6 files changed

+184
-1
lines changed

.github/workflows/native-image-build-shared-library.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
timeout-minutes: 15
2121
strategy:
2222
matrix:
23-
java-version: ['21', 'dev']
23+
java-version: ['21', '24-ea']
2424
steps:
2525
- uses: actions/checkout@v4
2626
- uses: graalvm/setup-graalvm@v1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: native-image/build-with-js-embedded
2+
on:
3+
push:
4+
paths:
5+
- 'native-image/build-with-js-embedded/**'
6+
- '.github/workflows/native-image-build-with-js-embedded.yml'
7+
pull_request:
8+
paths:
9+
- 'native-image/build-with-js-embedded/**'
10+
- '.github/workflows/native-image-build-with-js-embedded.yml'
11+
schedule:
12+
- cron: "0 0 1 * *" # run every month
13+
workflow_dispatch:
14+
permissions:
15+
contents: read
16+
jobs:
17+
run:
18+
name: Run 'native-image/build-with-js-embedded'
19+
runs-on: ubuntu-latest
20+
timeout-minutes: 15
21+
strategy:
22+
matrix:
23+
java-version: ['21', '24-ea']
24+
steps:
25+
- uses: actions/checkout@v4
26+
- uses: graalvm/setup-graalvm@v1
27+
with:
28+
java-version: ${{ matrix.java-version }}
29+
distribution: 'graalvm'
30+
github-token: ${{ secrets.GITHUB_TOKEN }}
31+
cache: 'maven'
32+
native-image-job-reports: 'true'
33+
- name: Run 'native-image/build-with-js-embedded'
34+
run: |
35+
cd native-image/build-with-js-embedded
36+
./run.sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Build a Polyglot Native Executable (Java and JavaScript)
2+
3+
You can find the steps to run this demo on [the website](https://www.graalvm.org/latest/reference-manual/native-image/guides/build-polyglot-native-executable/).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>PrettyPrintJSON</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<maven.compiler.source>21</maven.compiler.source>
14+
<maven.compiler.target>21</maven.compiler.target>
15+
<graalvm.polyglot.version>24.1.1</graalvm.polyglot.version>
16+
<native.maven.plugin.version>0.10.3</native.maven.plugin.version>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.graalvm.polyglot</groupId>
22+
<artifactId>polyglot</artifactId>
23+
<version>${graalvm.polyglot.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.graalvm.polyglot</groupId>
27+
<artifactId>js</artifactId>
28+
<version>${graalvm.polyglot.version}</version>
29+
<type>pom</type>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<pluginManagement>
35+
<plugins>
36+
<plugin>
37+
<groupId>org.apache.maven.plugins</groupId>
38+
<artifactId>maven-jar-plugin</artifactId>
39+
<version>3.4.2</version>
40+
<configuration>
41+
<archive>
42+
<manifest>
43+
<mainClass>com.example.PrettyPrintJSON</mainClass>
44+
</manifest>
45+
</archive>
46+
</configuration>
47+
</plugin>
48+
</plugins>
49+
</pluginManagement>
50+
</build>
51+
52+
<profiles>
53+
<profile>
54+
<id>native</id>
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.graalvm.buildtools</groupId>
59+
<artifactId>native-maven-plugin</artifactId>
60+
<version>${native.maven.plugin.version}</version>
61+
<extensions>true</extensions>
62+
<executions>
63+
<execution>
64+
<id>build-native</id>
65+
<goals>
66+
<goal>compile-no-fork</goal>
67+
</goals>
68+
<phase>package</phase>
69+
</execution>
70+
</executions>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
</profile>
75+
</profiles>
76+
77+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
set -ex
3+
4+
mvn --no-transfer-progress package
5+
mvn -Pnative package
6+
./target/PrettyPrintJSON <<EOF
7+
{"GraalVM":{"description":"Language Abstraction Platform","supports":["combining languages","embedding languages","creating native images"],"languages": ["Java", "JavaScript", "Python"]}}
8+
EOF
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.example;
42+
43+
import java.io.*;
44+
import java.util.stream.*;
45+
import org.graalvm.polyglot.*;
46+
47+
public class PrettyPrintJSON {
48+
public static void main(String[] args) throws java.io.IOException {
49+
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
50+
String input = reader.lines()
51+
.collect(Collectors.joining(System.lineSeparator()));
52+
try (Context context = Context.create("js")) {
53+
Value parse = context.eval("js", "JSON.parse");
54+
Value stringify = context.eval("js", "JSON.stringify");
55+
Value result = stringify.execute(parse.execute(input), null, 2);
56+
System.out.println(result.asString());
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)