返回文章列表文章索引阅读全文
STARBLOG / 阅读手记
C#

C# uses iKvm advanced technology to seamlessly integrate into the JVM ecosystem.

C# developers using iKVM to mooch off the JVM ecosystem? It's convenient, but dependency handling still requires manual effort. Seamless integration? It's just converting JARs to DLLs, saving the trouble of migrating code. Lacking AI tools? Just invoke HanLP, but dependency management... heh.

曦远 (DealiAxy)约 11 分钟
目录7 个章节

Preface

Time flies, and before I knew it, the National Day holiday was almost over. If I didn’t update my blog soon, I’d really be slacking off...

Recently, while working on AIHub, I was looking for a Named Entity Recognition (NER) library that I could use in C#, but I couldn’t find one. In terms of the AI ecosystem, C# isn’t exactly rich—it’s still more of a Python thing. However, I didn’t want to go down the gRPC route like with LLMs; it felt a bit cumbersome.

That’s when I realized that the JVM ecosystem actually has quite a few NLP tools, like Stanford NLP and HanLP. So, I thought of IKVM that I’d seen online before—I could just borrow the JVM ecosystem directly! 😃

About iKvm

Check the official introduction

IKVM is an implementation of Java for the Microsoft .NET platform. It can be used to quickly and easily:

  • Execute compiled Java code (bytecode) on .NET Framework or .NET Core
  • Convert bytecode to a .NET assembly to directly access its API in a .NET project

These tasks can be done without porting source code to .NET.

There are two working methods:

  • Directly call the jar package to execute in C#
  • Translate the jar package into a .NET platform dll, then reference and execute it

Generally, the second method is sufficient. The first one involves dynamic invocation and essentially provides no code hints, so I don't want to consider that approach.

iKvm is actually a comprehensive system. It includes a complete JDK standard library, runtime, and more. I gave it a quick look and found it has everything from Swing, XML, to media support. It even supports JDK reflection.

Furthermore, it comes with the modern build tool Maven!

PS: I'm not sure about Gradle; I haven't tried that yet.

Regarding Dependency Handling

While iKvm offers convenient Maven support, it cannot handle dependencies within a package!

For example, if you reference the StarAI package, which depends on the Transformer library, Maven would automatically download all dependencies for the build.

However, iKvm's Maven integration cannot automatically resolve dependencies. Therefore, you must manually add both the StarAI and Transformer libraries to the configuration.

Getting Started

This article uses HanLP as an example

Prerequisites

First, add two NuGet dependencies.

dotnet add package IKVM
dotnet add package IKVM.Maven.Sdk

Or directly edit the project file.

<ItemGroup>
    <PackageReference Include="IKVM" Version="8.6.4" />
    <PackageReference Include="IKVM.Maven.Sdk" Version="1.5.5" />
</ItemGroup>

Then, add the Maven dependency in the project file. You can just copy it directly from the Maven repository—very convenient!

For those unfamiliar with Java, here's a link: https://mvnrepository.com/

<ItemGroup>
    <MavenReference Include="hanlp">
        <groupId>com.hankcs</groupId>
        <artifactId>hanlp</artifactId>
        <version>portable-1.8.4</version>
    </MavenReference>
</ItemGroup>

After saving, the IDE will automatically perform operations. It will download the dependencies IKVM needs, the JDK and runtimes for various platforms, and will also automatically download the JAR from Maven and transpile (transpile) it into a .NET platform DLL.

This process will take some time. Please wait patiently.

If it doesn't execute automatically, run the following manually.

dotnet restore
dotnet build

Getting Started with Coding

Here we use HanLP's sentence component analysis feature as an example.

using com.hankcs.hanlp.model.crf;
using com.hankcs.hanlp.model.perceptron;
using com.hankcs.hanlp.seg;
using com.hankcs.hanlp.seg.common;

namespace AIHub.Algo.HanLP;

public class NER {
    private readonly string _modelPath;

    public NER(string modelPath) {
        _modelPath = modelPath;
    }

    public void Recognize(string input) {
        PerceptronLexicalAnalyzer analyzer = new PerceptronLexicalAnalyzer(
            Path.Combine(_modelPath, "cws.bin"),
            Path.Combine(_modelPath, "pos.bin"),
            Path.Combine(_modelPath, "ner.bin")
        );

        var result = analyzer.analyze(input);
        Console.WriteLine(result);
    }
}

For testing, simply call the Recognize method.

References

_EOF_

C# uses iKvm advanced technology to seamlessly integrate into the JVM ecosystem.

本文作者:
程序设计实验室
版权声明:
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!

相关文章

SharpIco:用纯C#打造零依赖的.ico图标生成器,支持.NET9与AOT编译

SharpIco 是一款纯 C# 实现的跨平台图标生成工具,支持 AOT 编译,无需外部依赖。它能将 PNG 图片分解为多尺寸(16x16至512x512)合成 ICO 图标,并支持图标结构分析。工具提供命令行界面,可通过 dotnet tool 一键安装,并已发布至 NuGet 和 Scoop。项目旨在简化图标生成与验证,追求轻量、纯粹、易集成的使用体验。

2025-05-26 · C# · 约 1 分钟

讨论

共 0 条

良言一句三冬暖,恶语伤人六月寒。

加载评论中…