By downloading these archives, you agree to the terms and conditions for accessing or otherwise using Python.
This directory contains the JavaCPP Presets module for:
- CPython 3.7.7 https://www.python.org/
Please refer to the parent README.md file for more detailed information about the JavaCPP Presets.
Java API documentation is available here:
∗ Call Py_AddPath(cachePackages())
before calling Py_Initialize()
.
Here is a simple example of CPython ported to Java from this C source file:
We can use Maven 3 to download and install automatically all the class files as well as the native binaries. To run this sample code, after creating the pom.xml
and Simple.java
source files below, simply execute on the command line:
$ mvn compile exec:java
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.bytedeco.cpython</groupId>
<artifactId>simple</artifactId>
<version>1.5.3-SNAPSHOT</version>
<properties>
<exec.mainClass>Simple</exec.mainClass>
</properties>
<dependencies>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>cpython-platform</artifactId>
<version>3.7.7-1.5.3-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>.</sourceDirectory>
</build>
</project>
import org.bytedeco.javacpp.*;
import org.bytedeco.cpython.*;
import static org.bytedeco.cpython.global.python.*;
public class Simple {
public static void main(String[] args) throws Exception {
Py_AddPath(cachePackages());
Pointer program = Py_DecodeLocale(Simple.class.getSimpleName(), null);
if (program == null) {
System.err.println("Fatal error: cannot get class name");
System.exit(1);
}
Py_SetProgramName(program); /* optional but recommended */
Py_Initialize();
PyRun_SimpleStringFlags("from time import time,ctime\n"
+ "print('Today is', ctime(time()))\n", null);
if (Py_FinalizeEx() < 0) {
System.exit(120);
}
PyMem_RawFree(program);
System.exit(0);
}
}