Calling Java code in C #

Source: Internet
Author: User
Tags zip

Download IKVM Required Components to IKVM's official website

Ikvm-0.40.0.1.zip

Ikvmbin-0.40.0.1.zip

Openjdk6-b12-stripped.zip

Unzip the Ikvm-0.40.0.1.zip and add%ikvm_home%\bin to the path. The%ikvm_home% here refers to the home directory of the IKVM after decompression.

Download IKVM.OpenJDK.ClassLibrary.dll to Debian IKVM.

Or go to rayfile to download data.tar.gz. This file can be extracted directly with WinRAR.

You need to download a simple tool Undeb to extract the. deb file.

You can also download to Rayfile

Extract the Ikvm_0.38.0.2+dfsg-2_all.deb file.

Create a new Java file package com.helloikvm.simple;

public class SimpleReturnString {
    public String returnString() {
        return "Hello, IKVM!";
    }

    public void throwException() throws Exception {
        throw new Exception("Hello, Exception!");
    }
}

Compile the simplereturnstring file, and then package it into a jar file, such as Ikvm.jar.

Use Ikvmc-out:ikvm.dll Ikvm.jar. You should now see a IKVM.dll file under the IKVM directory.

Create a new C #. NET project, first add the necessary DLLs

%IKVM_HOME%\BIN\IKVM. OpenJDK.Core.dll

%IKVM_HOME%\BIN\IKVM. Runtime.dll

%IKVM_HOME%\BIN\IKVM. Runtime.JNI.dll

IKVM. The DLL for this openjdk.classlibrary.dll[is in the Ikvm_0.38.0.2+dfsg-2_all.deb\usr\lib\cli folder. Or it can be found in the usr\lib\cli of the data.tar.gz. See step three and step fourth]

Add the 10th step generated IKVM.dll to C #. NET project, then a call to Java can be implemented using%package_name%.

Write C # code//import Java Package

using com.helloikvm.simple;

namespace IKVMTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnTest_Click(object sender, EventArgs e)
        {
            // Initialize the Java method
            SimpleReturnString simpler = new SimpleReturnString();
            // Call Java method
            String returnString = simpler.returnString();
            txtTest.Text = returnString;
        }
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.