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

Kotlin命令行编译报错:无法导入包,出现未解析引用错误

Fixing "unresolved reference: ConnectionHandler" & Missing META-INF in Kotlin Command-Line Compilation

Let's break down what's going wrong here and fix it step by step:

1. Why the "unresolved reference" error pops up

When you run kotlinc main.kt -include-runtime -d main.jar, you're only telling the compiler to process main.kt—it has no clue about the ConnectionHandler.kt and HttpRequest.kt files sitting in the server directory. Even though those files declare the server package, the compiler can't find them unless you explicitly include them in the compilation command. That's exactly why it's throwing the "unresolved reference" error: it doesn't know the server.ConnectionHandler class exists at all.

2. Fix the compilation command

You need to include all your Kotlin source files in the kotlinc command. Assuming your directory structure looks like this:

.
├── main.kt
└── server
    ├── ConnectionHandler.kt
    └── HttpRequest.kt

Use this corrected command instead:

kotlinc main.kt server/*.kt -include-runtime -d main.jar

If you end up adding subdirectories under server later, you can use a recursive wildcard to grab all Kotlin files automatically:

kotlinc main.kt server/**/*.kt -include-runtime -d main.jar

Also double-check that in main.kt, you're correctly importing the class you need:

import server.ConnectionHandler

3. Fixing the missing META-INF folder

The missing META-INF directory is just a side effect of the compilation failure—when the compiler hits that unresolved reference error, it stops dead before generating the jar file (and its associated META-INF content). Once you fix the compilation command and the build succeeds, the kotlinc tool will automatically generate the META-INF folder inside your main.jar (especially since you're using -include-runtime, which adds the Kotlin runtime and necessary manifest entries).

If you need to customize the META-INF content (like specifying a main class manually), here's how:

  1. Create a META-INF directory in your project root, then add a MANIFEST.MF file inside it with content like:
Main-Class: YourMainClassName

Replace YourMainClassName with the fully qualified name of your main class (e.g., if your main class is in the default package and named Server, just use Server; if it's in a package like com.example, use com.example.Server).
2. Update your compilation command to include the manifest:

kotlinc main.kt server/*.kt -include-runtime -manifest META-INF/MANIFEST.MF -d main.jar

Once you run the corrected compile command without errors, your main.jar will include the META-INF folder with all the necessary files.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:17:02