Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Trouble Accessing Java Classes from Python Using GraalPy and GraalVM#448

Discussion options

Hello everyone,

What I want to do

I'm trying to access a Java class from a Python script using GraalPy (GraalPython) with GraalVM, but I'm encountering an error that I can't seem to resolve.

Python Script (main.py)

import java# Get the Java classpath and print itSystem = java.type("java.lang.System")classpath = System.getProperty("java.class.path")print(f"Java Classpath: {classpath}")# Attempt to access the Java classUser = java.type('com.example.model.User')# Create an instance of the Java classuser = User('Alice')print(user.getName())

Java Class(User.java)

package com.example.model;public class User {    private String name;    public User(String name) {        this.name = name;    }    public String getName() {        return this.name;    }}

I've compiled the Java class and packaged it into a JAR file named gpdemo-0.0.1-SNAPSHOT-plain.jar, and I set it as classpath for zshrc.

Execution command

graalpy --jvm \  --vm-cp=$CLASSPATH \  ./main.py

Error Message:

User = java.type('com.example.model.User')KeyError: 'host symbol com.example.model.User is not defined or access has been denied'

What I've Tried:

  • Verified that the JAR file is in the correct location and that the class is properly packaged.
  • Printed out the Java classpath within the Python script to ensure that the JAR file is included.
  • Ensured that GraalVM is installed correctly and that I'm using the --jvm option to enable JVM interop.

Environment Details:

GraalVM Version: Oracle GraalVM 23.0.1+11.1
GraalPy Version: GraalPy 3.10.8 (GraalVM CE JVM 23.1.2)
Operating System: macOS

Questions:

  • Is there something I'm missing in the way I'm specifying the classpath when running the script?
  • Do I need to adjust any settings related to host access or permissions to allow Python to access the Java class?
  • Has anyone else encountered a similar issue and found a solution?
You must be logged in to vote

I tried your example and it printedAlice with no errors. What I did:

  • Save your java code ascom/example/model/User.java and your python code asmain.py
  • javac com/example/model/User.java
  • graalpy --vm.cp=. main.py

The differences I have from you is that I used the latest release, I'm on Linux and I didn't build a JAR. Could you please try the latest release (24.1.1, thegraalpy-jvm variant)?

Replies: 2 comments 3 replies

Comment options

additionally, here is the contained classes which are showed by
jar tf $CLASSPATH
command.

MacBook-Pro-4 gpdemo % jar tf $CLASSPATHMETA-INF/META-INF/MANIFEST.MFcom/com/example/com/example/demo/com/example/demo/GpdemoApplication.classcom/example/model/com/example/model/User.classapplication.properties
You must be logged in to vote
0 replies
Comment options

Hi@ShuzoShinagawa1102, the option for supplying classpath when running through the graalpy launcher is--vm.cp=$CLASSPATH (not-vm-cp).

You must be logged in to vote
3 replies
@ShuzoShinagawa1102
Comment options

@msimacek Thank you for pointing that out. It was just a typo on my part, and I am indeed using "--vm.cp" when runnng the command.

@msimacek
Comment options

I tried your example and it printedAlice with no errors. What I did:

  • Save your java code ascom/example/model/User.java and your python code asmain.py
  • javac com/example/model/User.java
  • graalpy --vm.cp=. main.py

The differences I have from you is that I used the latest release, I'm on Linux and I didn't build a JAR. Could you please try the latest release (24.1.1, thegraalpy-jvm variant)?

Answer selected bymsimacek
@ShuzoShinagawa1102
Comment options

@msimacek
Thank you for your kind response.

I installed GraalPy latest release version (24.1.1) that you suggested (https://github.com/oracle/graalpython/releases) and it worked.
The command which I executed is here.

graalpy --jvm \  --vm.cp=gpdemo-0.0.1-SNAPSHOT-plain.jar \  ./main.py

it returned ↓
スクリーンショット 2024-10-30 22 54 57

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@ShuzoShinagawa1102@msimacek

[8]ページ先頭

©2009-2025 Movatter.jp