Winform calls the jar package and winform calls the jar package.

Source: Internet
Author: User

Winform calls the jar package and winform calls the jar package.

Because of work needs, need to do a data upload program, the customer stipulates: data interface using http connection, using JSON-RPC lightweight Remote Call protocol. Therefore, we decided to use winform as a management interface (including other functions), java to complete data transmission, and winform to call the jar package to meet customer requirements.

The specific procedure is as follows (refer to the example ):

1. Package the compiled Class file in java; Package the command JAR

For example, you can package all the class folders in a directory;

Command Used: jar cvf test. jar-C com /.

Here, test. jar is the jar package to be generated; com/. Is the folder under the specified current directory, which includes subfolders and class files;

2. Download the components required by IKVM from the IKVM Official Website: http://www.ikvm.net/#go directly to the download page: https://sourceforge.net/projects/ikvm/files/

Ikvmbin-7.2.4630.5.zip

Sorry, currently, I think ikvm only supports 1.7. I don't know if it will be updated later. Why did it stop after ?)

3. Set the path

Decompress ikvmbin-7.2.4630.5.zip and add the IKVM_HOME variable to the environment variable, as shown in figure

Add % IKVM_HOME %/bin to path. Here % IKVM_HOME % refers to the main directory of ikvm after decompression.

4. convert a java jar package to A. dll Control

Command: ikvmc-out: IKVM. dll test. jar

Here, IKVM. dll is the name of the. dll Control to be generated, and test. jar is the previously packaged jar package.

Successful output:

IKVM. NET Compiler version 7.2.4630.5
Copyright (C) 2002-2012 Jeroen Frijters
Http://www.ikvm.net/

5. Add the required controls to the winform project.

1. Create a C # winform project. First, add the required DLLs (I added all the dll files in bin to the reference to save some trouble)

Method: click "Reference"> "add reference"> "Browse" to add the dll.

Some blogs say that IKVM. OpenJDK. Core. dll, IKVM. Runtime. dll, IKVM. Runtime. JNI. dll need to be added.

2. Add the generated. dll file

Reference the previously generated. dll file to the project (Note that if the dll is updated to the latest version, update it to the reference folder where the reference is added)

Vi. Test

Use the java class in the winform project. The method is the same as java. However, the C # syntax using is used for package reference.

 

Source code:

Java source code:

Package com. zht;
// Java class to be called
Public class Test {
// The Java method to call
Public String returnString (){
Return "Hello, zht! ";
}

}

 

C # form source code:

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using com. zht;

Namespace KIVMTest
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Private void Form1_Load (object sender, EventArgs e)
{
Test t = new Test ();
String str = t. returnString ();
MessageBox. Show (str );
}
}
}

 

Result:

After the winform window is started, a prompt window is displayed with the following content: Hello, zht!

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.