You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用GraalVM native-image将Java企业应用打包为单目录?

How to Package GraalVM Native Image as a Single Directory (Like PyInstaller --onedir)

Great question! I’ve dealt with this exact need for large enterprise applications using GraalVM, so let’s break down exactly how to get that single-directory output you’re looking for—no single monolithic executable, just all your app files, DLLs, and dependencies neatly contained in one folder.

Step 1: Use GraalVM’s Path and Name Flags

The key here is overriding GraalVM’s default behavior of outputting a single executable with two critical flags:

  • -H:Path=<target-directory>: Specifies the directory where all your app files will be placed.
  • -H:Name=<executable-name>: Sets the name of your main application executable inside that directory.

Here’s a concrete example command:

native-image -jar your-enterprise-app.jar -H:Path=./my-app-dist -H:Name=app-launcher

After running this, you’ll get a my-app-dist directory containing:

  • app-launcher (or app-launcher.exe on Windows): Your main executable
  • All required shared libraries (DLLs on Windows, .so/.dylib on Linux/macOS)
  • Any bundled resources your app needs

Step 2: Include Additional Resources (If Needed)

If your app relies on external config files, static assets, or other resources, use the -H:IncludeResources flag to ensure they’re copied into the directory. For example, to include all files under config/ and static/:

native-image -jar your-enterprise-app.jar -H:Path=./my-app-dist -H:Name=app-launcher -H:IncludeResources="config/.*|static/.*"

This ensures those resources are packaged alongside your executable, so your app can access them just like it would in a regular JVM environment.

Step 3: Handle Custom Shared Libraries

If your app uses custom DLLs or shared libraries (not automatically detected by GraalVM), use the -H:AddSharedLibs flag to explicitly include them. For example:

native-image -jar your-enterprise-app.jar -H:Path=./my-app-dist -H:Name=app-launcher -H:AddSharedLibs=./libs/custom.dll

GraalVM will copy these libraries into the target directory and ensure the executable references them correctly.

Step 4: Test the Distribution

Once the directory is generated, navigate into it and run your executable directly:

  • Linux/macOS: cd my-app-dist && ./app-launcher
  • Windows: cd my-app-dist && app-launcher.exe
    All dependencies are contained within the directory, so you can zip up the whole folder and deploy it anywhere without worrying about missing libraries.

Troubleshooting Tips

  • If some dependencies are missing, run the command with the -v flag to get verbose output—this will show you which libraries GraalVM is detecting (or missing).
  • Use -H:ReportExceptionStackTraces if you run into runtime errors, to get detailed stack traces for debugging.

内容的提问来源于stack exchange,提问作者tofuthefirst

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 20:58:02