. Net calls Java Implementation Method,. net calls java

Source: Internet
Author: User

. Net calls Java Implementation Method,. net calls java

 

I. IKVM1.1 download and configuration IKVM

1.1.1.Download path

Http://www.ikvm.net/index.html

1.1.2.Set path

Decompress ikvm-0.42.0.3.zip and add % IKVM_HOME %/bin to path. Here % IKVM_HOME % refers to the main directory of ikvm after decompression.

 

1.2 Method 1: directly convert the. class file to. dll

Website Introduction

Http://www.codeproject.com/Articles/13549/Using-Java-Classes-in-your-NET-Application

Input the command in the dosinterface and go to the directory of the ikvm.exe application;

At the same time, put the. class file to be converted (such as the testmain. class file) in the directory and enter the following command in dos:

Ikvmc <space>-target: library <space> testmain. class

You can view the generated. dll file in the C: \ Program Files \ ikvm \ bin directory.

 

The link is as follows:

1.3 Method 2: Convert the jar package of a Java project to. dll

C#directly calls Java code and uses ikvmbin-0.44.0.5.zip

1. Download and decompress

2. Configure environment variables:

Add % IKVM_HOME % \ bin in path, and IKVM_HOME is the main directory of the extracted IKVM. On the dos interface, enter the "ikvm" command to test whether the configuration is successful.

3. Write java code (the java code should be placed under the specified package, rather than under the default package ):

 1 package com.xyy.csharp;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 public class TestMain {
 7 
8 // obtain the set
 9     public List<Demo1> getLists(){
10         List<Demo1> lst=new ArrayList<Demo1>();
11         Demo1 d=new Demo1();
12         d.setId(1);
13         d.setName("admin");
14         Demo1 d2=new Demo1();
15         d2.setId(2);
16         d2.setName("sa");
17         
18         lst.add(d);
19         lst.add(d2);
20         return lst;
21     }
22     
23 // obtain the string
24     public String getString(){
25 return "I Am a string ";
26     }
27     
28 // obtain the object
29     public Demo1 getObject(){
30         Demo1 demo=new Demo1();
31         demo.setId(1);
32         demo.setName("admin");
33         return demo;
34     }
35     
36 // obtain boolean
37     public boolean getBoolean(){
38         return Boolean.TRUE;
39     }
40     
41 // output on the console
42     public void SystemOutPrint(){
43 System. out. println ("I am the console output statement ");
44     }
45 }

5. compress the java project into a jar package (test. jar). For details about the packaging process, see the remarks:
6. input the command in the dosinterface, change the cdns to the directory of the ikvm.exe application;

At the same time, put the. jar file to be converted (such as the ikvm. jar file) in the directory, enter the command to convert the ikvm. jar file to the zhou. dll file:

 

IkvmCommand Format:Ikvmc <Space>-Out:Aaa. dll<Space>Bbb. jar

Bbb. jar indicates the. jar file to be converted, and aaa. dll indicates the. dll file name to be generated.

7. Create a c # project and introduce the following three dll files:

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

% IKVM_HOME % \ bin \ IKVM. Runtime. dll

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

Introduce the just-packaged IKVM. dll

8.

9. output result:

10. c # The collection returned by java cannot be directly received. I have not studied it in depth here.

Note: The JAVA project package into a jar package process

Http://www.cnblogs.com/myCodingSky/p/3347511.html

Jar packages for java projects can be divided into two situations:

1. the java project has not imported a third-party jar package

1. Open the project in Eclipse, right-click the project, and select "Export ";
2. Select Java/JAR file and Next;
3. In Select the resources to export, you can Select the project folder you want to include. Some unnecessary folders do not need to be put in, so as not to increase the space;
There are several options:
* Export generated classfiles and resources indicates that only generated. class files and other resource files are exported.
* Export all output folders for checked projects: Export all folders of the selected project
* Export java source file and resouces indicates that the exported jar package will contain your source code *. java. If you do not want to disclose the source code, do not select this option.
* Export refactorings for checked projects include some reconstructed information files.


In Selectthe export destination, select the path of the exported jar, and Next
4. On the next page, you can choose whether to export the *. class files containing warning warnings or error errors. Ignore him, Next
5. Configure the project on the next page. (If a third-party jar package is not introduced to the project, we will use the default system option. Mainclass must be set as the main method entry class of your project)
* Generate the manifest file is used by the system to automatically Generate the MANIFEST. MF file. If your project does not reference other class-path files, you can select this item.
* Use existing mainfest from workspace. You can select a custom. MF file in the format written above.
* Seal content must encapsulate the entire jar or the specified packet.
* Main class you can select your program entry here. The jar package in the future will be the execution result of your entry class.
Finish .......

2. the java project has imported third-party Jar packages (for example, the project has imported the sqlserver2005.jar package)

Packaging is a little more complicated at this time:

The preceding four steps are the same as the above, except that you need to select the Useexisting mainfest from workspace option in step 1 and select a MANIFEST created by yourself. MF file, because the system selects the Generate the manifest file option by default, and this option does not include third-party jar packages used by the project into the jar package generated by the java project.

Custom configuration file MANIFEST. MF:

First, create a MANIFEST. MF file under the root directory of the project.

Next, open the file in notepad and edit it:

For example:
Manifest-Version: 1.0
Main-Class: StudentManager_Test
Class-Path: sqlserver2005.jar
The Manifest-Version indicates the program Version, and the Main-Class indicates the Main method entry Class of the program. The slass-Path specifies the location of the external jar package. Note that we assume that our project is packaged as a project. jar, according to the above definition, it should be in the project. jar has related jar packages (sqlserver2005.jar) in the same-layer directory. If multiple third-party jar packages are introduced, they can be separated by spaces. Otherwise, the Exception in thread "main" java. lang. NoClassDefFoundError error may occur.

Ii. Web Service

 BuildJavaVersionWebService 

2.1 What is WebServices

It is a universal model for building applications and can be run in any operating system that supports network communication. It is a new Web Application Branch, it is a self-contained, self-describing, and modular application that can be released, located, and called through the Web. Web Service is an application component that logically provides data and services for other applications. each application accesses the Web Service through the network protocol and some standard data formats (Http, XML, and Soap). The results are obtained through the internal execution of the Web Service. web services can execute any function from simple requests to complex business processing. After deployment, other Web Service applications can discover and call the services deployed by the application.

Key Technologies and rules

The following key technologies and rules are used to build and use Web services:

1. XML: Standard Method for describing data.

2. SOAP: indicates the Information Exchange Protocol.

3. WSDL: Web Service Description Language.

4. UDDI: Universal Description, discovery, and integration. It is a platform-independent, XML-based protocol used to describe commerce on the Internet.

XML

The Extensible Markup Language (XML) is the basic format for data representation on the Web Service platform. In addition to ease of establishment and analysis, XML has the primary advantage of being platform-independent and vendor-independent. Independence is more important than technical superiority: software vendors will not choose a technology invented by competitors.

SOAP

SOAP is the standard communication protocol of Web Service, abbreviated as simple objectaccess protocoll, simple Object Access Protocol. It is a standardized XML Message format for message transmission.

WSDL

The full name of WSDL is web service Description Language, which is a web service Description Language Based on XML format. The main purpose is that the web service provider transfers all relevant content of its web service, such as the transmission mode, service method interface, interface parameters, and service path of the provided service, generate a complete document and publish it to the user. You can use this WSDL document to create a SOAP request message and send it to the webservice provider over HTTP. After the web service completes the service request, it returns the SOAP Response Message to the requester, the service requestor then parses the SOAP returned message into something that he can understand based on the WSDL document.

UDDI

Register and publish web Services. UDDI is a standard for creating registry services, so that you can register and publish your web services for user search. however, when a service provider wants to publish its web service to the world to find its service externally, the service provider can register its web service to the corresponding UDDI commercial registration website, at present, there are four UDDI commercial registration websites around the world, such as IBM. Because the url uri of the web service has been given in the WSDL file, the external interface can call the corresponding web service directly through the URI provided by the WSDL. Therefore, UDDI is not a necessary web service component, and the service provider can not register it.

2.2 create and publish your own WebService

1. Install MyEclipse

2. Configure Tomcat

Open MyEclipse

Menu Bar-> File-> New-> Web Service Project

Here, we use the JAX-WS Method

JAX-WS

Jax-WS is only available in Java. The new Web Service mode is used to configure Web Services Based on annotations. It is similar to the Web Service in Asp, which is much more difficult than Xfire.

REST (JAX-RS)

Use less

Xfire(Expired)

Tedious Configuration

Create packages and class libraries in the Service to implement your own business.

We created a user management system, which naturally needs to be used to manage users.

Publish a Web Service

After WS is published, configure TomCat.

In the displayed form, expand MyEclipse> Servers> Tomcat.

Right-click the project and choose add library file.

At this point, all steps start deployment after configuration is complete.

Start TomCat

Find the deployment icon in the toolbar.

Deployment complete. Open IE for testing.

2.3 create a client for testing

In this case, the library file required by WS is generated in the project.

Create a call test class

At this point, our Web Service configuration + test is complete. In fact, it is not difficult. Even if there are many steps, it is complicated and easy to forget, you can use it as a memorandum for future reference!

 

References: http://www.cnblogs.com/lanxuezaipiao/p/3291641.html

Http://blog.csdn.net/hehainan_86/article/details/28621989

 


. Net Method for calling JAVA APIs

JNI (java Native interface) not only provides the ability of java to call c or c ++ dll files, but also reverse calls. We recommend that you check this. c # can also generate dll files. It should be okay. This is not clear.

If you want to call the JAVA WebService Interface in NET, how can you do this without adding references?

I have not tried it. However, there are some existing practices on the Internet. For details, refer to: blog.csdn.net/...023039.
Or Baidu search:. net web service does not add reference
 

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.