{"id":18485283,"url":"https://github.com/softinstigate/classgraph-on-graalvm","last_synced_at":"2025-04-19T15:11:00.344Z","repository":{"id":87382199,"uuid":"313648464","full_name":"SoftInstigate/classgraph-on-graalvm","owner":"SoftInstigate","description":"This repo demonstrates how to build a native image of a java application that uses ClassGraph with GraalVM","archived":false,"fork":false,"pushed_at":"2020-11-17T14:47:59.000Z","size":14,"stargazers_count":5,"open_issues_count":2,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-29T09:11:47.643Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SoftInstigate.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-11-17T14:41:46.000Z","updated_at":"2025-03-03T09:03:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"16164c54-68a5-4d41-9de2-065406bc99fa","html_url":"https://github.com/SoftInstigate/classgraph-on-graalvm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftInstigate%2Fclassgraph-on-graalvm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftInstigate%2Fclassgraph-on-graalvm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftInstigate%2Fclassgraph-on-graalvm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftInstigate%2Fclassgraph-on-graalvm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SoftInstigate","download_url":"https://codeload.github.com/SoftInstigate/classgraph-on-graalvm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249723180,"owners_count":21315962,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T12:44:44.475Z","updated_at":"2025-04-19T15:11:00.321Z","avatar_url":"https://github.com/SoftInstigate.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Using ClassGraph on GraalVM\n\nThis repo demonstrates how to build a native image of a java application that uses [ClassGraph](https://github.com/classgraph/classgraph) with [GraalVM](https://github.com/oracle/graal).\n\n## Build and test\n\nInstall GraalVM\n\n```bash\n$ mvn clean package\n```\n\nExecute the java image\n\n```bash\n$ java -jar target/classgraph-on-graalvm-1.0-SNAPSHOT.jar\nInitializing class com.softinstigate.App\nannotated classes [com.softinstigate.FindMe]\n\tannotated method executeMe\nExecuting targets [{ com.softinstigate.FindMe, executeMe }]\n\tclass annotation name: findMe\n\tclass annotation priority: 10\n\tmethod annotation name: executeMe\n\tmethod annotation priority: 10\nHello World!!\n```\n\nExecute the native image\n\n```bash\n$ ./target/cog\nExecuting targets [{ com.softinstigate.FindMe, executeMe }]\n\tclass annotation name: findMe\n\tclass annotation priority: 10\n\tmethod annotation name: executeMe\n\tmethod annotation priority: 10\nHello World!!\n```\n\n## The issue\n\n`ClassGraph.scan()` simply doesn't work at runtime on a native image. However it does work at build time (see [Build-Time-Scanning](https://github.com/classgraph/classgraph/wiki/Build-Time-Scanning) on ClassGraph wiki).\n\n## Approach\n\nThe `native-image.properties` includes the following option:\n\n```\n--initialize-at-build-time=com.softinstigate.App,io.github.classgraph.,nonapi.io.github.classgraph.\n```\n\n`com.softinstigate.App` performs the class scanning at class initialization time defining:\n\n```java\npublic class App {\n    static {\n        // here executes ClassGraph.scan()\n    }\n}\n```\n\nSince the class `com.softinstigate.App` is initialized at build time by `native-image`, the scanning occurs.\n\nThe found classes are saved to a static field and used in the class. The example code looks for classes and methods with given annotations and them invoke the methods using reflection.\n\nSince reflection is used, native-image must run properly configured. The following section provides instructions on how to automatically generate the configuration.\n\nSee [Reflection Use in Native Images](https://www.graalvm.org/reference-manual/native-image/Reflection/) from GraalVM documentation.\n\n## Generate native-image configuration\n\nRun the app with the `native-image-agent`\n\n```\n$ mvn clean package\n$ java -agentlib:native-image-agent=config-merge-dir=src/main/resources/META-INF/native-image/com.softinstigate/classgraph-on-graalvm/ -jar target/classgraph-on-graalvm-1.0-SNAPSHOT.jar\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftinstigate%2Fclassgraph-on-graalvm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftinstigate%2Fclassgraph-on-graalvm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftinstigate%2Fclassgraph-on-graalvm/lists"}