Skip to content

Commit 23d75f3

Browse files
committed
[GR-53617] Expand docs for Java embeddings
PullRequest: graalpython/3308
2 parents 14a6d23 + 89a99f9 commit 23d75f3

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed

docs/user/README.md

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Go [here](Python-Runtime.md) to get started with GraalPy as CPython replacement.
1414

1515
You can use GraalPy with GraalVM JDK, Oracle JDK, or OpenJDK.
1616
You can easily add GraalPy to your Java application using Maven or Gradle build tools as shown below.
17+
Other build systems (Ant, Make, CMake, ...) can also be used with a bit more manual work.
1718

1819
## Maven
1920

@@ -43,9 +44,32 @@ The project uses the [GraalVM SDK Polyglot API](https://www.graalvm.org/sdk/java
4344
The Java code and the _pom.xml_ file are heavily documented and the generated code describes available features.
4445
(If you do not wish to use Maven, the archetype Java code also provides guidance to create a custom embedding.)
4546

47+
### Creating Cross-platform JARs with Native Python Packages
48+
49+
The generated project uses the GraalPy Maven plugin, which makes it easy to add Python dependencies.
50+
However, Python packages may have native components that are specific to the build system.
51+
In order to distribute the resulting application for other systems, follow these steps:
52+
53+
1. Build the project on each deployment platform.
54+
Rename JAR files so they each have a platform-specific name and move them to a temporary directory on the same machine.
55+
56+
2. Unzip each of the JAR files (substituting the correct names for the JAR files).
57+
A special file, _vfs/fileslist.txt_ needs to be concatenated from each JAR file.
58+
Finally, create a new _combined.jar_ from the combination of all files and with the concatenated _fileslist.txt_.
59+
60+
```bash
61+
unzip linux.jar -d combined
62+
mv combined/vfs/fileslist.txt fileslist-linux.txt
63+
unzip windows.jar -d combined
64+
mv combined/vfs/fileslist.txt fileslist-windows.txt
65+
cat fileslist-linux.txt fileslist-windows.txt > combined/vfs/fileslist.txt
66+
cd combined
67+
zip -r ../combined.jar *
68+
```
69+
4670
## Gradle
4771

48-
1. Create a Java application with Gradle using the provided command and follow the prompts (select a build script language, select a test framework, etc.):
72+
1. Create a Java application with Gradle using the command below and follow the prompts (select a build script language, select a test framework, and so on):
4973
```bash
5074
gradle init --type java-application \
5175
--project-name interop \
@@ -116,6 +140,53 @@ The Java code and the _pom.xml_ file are heavily documented and the generated co
116140
117141
> Note: The performance of the GraalPy runtime depends on the JDK in which you embed it. For more information, see [Runtime Optimization Support](https://www.graalvm.org/latest/reference-manual/embed-languages/#runtime-optimization-support).
118142
143+
## Ant, CMake, Makefile or Other Build Systems Without Direct Support for Maven Dependencies
144+
145+
Some (often older) projects may be using Ant, Makefiles, CMake, or other build systems that do not directly support Maven dependencies.
146+
Projects such as [Apache Ivy™](https://ant.apache.org/ivy/history/master/tutorial/start.html) enable such build systems to resolve Maven dependencies, but developers may have reasons not to use them.
147+
GraalPy comes with a tool to obtain the required JAR files from Maven.
148+
149+
1. Assuming there is some directory where third-party dependencies are stored for the project and that the build system is set up to put any JAR files there on the classpath, the project directory tree might look similar to this:
150+
151+
```
152+
├───lib
153+
│ └─── ... *.jar dependencies are here
154+
└───src
155+
└─── ... *.java files and resources are here
156+
```
157+
158+
2. [Install GraalPy](Python-Runtime.md#installing-graalpy) for your system and ensure you have `graalpy` on your `PATH`.
159+
Open a command-line interface and enter your project directory.
160+
Then, as appropriate for your system, run one of the following commands:
161+
162+
In a POSIX shell:
163+
```bash
164+
export GRAALPY_HOME=$(graalpy -c 'print(__graalpython__.home)')
165+
"${GRAALPY_HOME}/libexec/graalpy-polyglot-get" -a python -o lib -v "24.0.0"
166+
```
167+
168+
In PowerShell:
169+
```
170+
$GRAALPY_HOME = graalpy -c "print(__graalpython__.home)"
171+
& "$GRAALPY_HOME/libexec/graalpy-polyglot-get" -a python -o lib -v "24.0.0"
172+
```
173+
174+
These commands download all GraalPy dependencies into the _lib_ directory.
175+
176+
3. Provided that your build system is set up to pick up the JAR files from _lib_, the GraalPy embedding code below should work if put in an appropriate place in the project to run as the main class.
177+
178+
```java
179+
import org.graalvm.polyglot.*;
180+
181+
public class Hello {
182+
public static void main(String[] args) {
183+
try (var context = Context.newBuilder().option("engine.WarnInterpreterOnly", "false").build()) {
184+
System.out.println(context.eval("python", "'Hello Python!'").asString());
185+
}
186+
}
187+
}
188+
```
189+
119190
#### Related Documentation
120191
121192
- [Modern Python on the JVM](Python-on-JVM.md)

0 commit comments

Comments
 (0)