Http://web.chinaitlab.com/web/812577.html
This article will introduce a Java call in detail. net DLL method to meet special requirements of customers: "in Java projects, you must use the provided. net write DLL encryption mechanism!"
Environment and tools:
◆. NET Framework 3.5 C #
◆ Java jdk1.5, Tomcat 5.5
◆ Jacob-1.15-M3
Implementation example:
I. C # create COM components
Create a new class project named testcom
Code
Using system; using system. Collections. Generic;
Using system. LINQ; using system. text;
Using system. runtime. interopservices;
Namespace testcom
{[GUID ("E9BCF867-CD81-40d0-9591-ED28D1ED2B53")]
Public interface iencrypt
{[Dispid (1)]
String getencrypt (string STR, string str2 );
}
[GUID ("33a6e58d-e1f5-4b53-b2e2-03b9f8ba2fad "),
Classinterface (classinterfacetype. None)]
Public class encrypt: iencrypt
{Public encrypt (){}
Public String getencrypt (string STR, string str2)
{
Return "test |" + STR + "|" + str2;
}
}
}
Open the project --> properties menu, open assembly information in the application tag, and select make assembly com-visible. Switch to the build tab and select register for com InterOP.
Guid generation: Open Visual Studio command prompt and enter the guidgen command to call up the tool. Select registry format as the type, click New guid, and copy it out.
[Dispid (1)] is the identifier of the function. If there are multiple functions, add [dispid (2)], [dispid (3)]…
Testcom. dll and testcom. TLB are generated in the DEBUG directory of the Compilation Program.
Manually register COM:
Open Visual Studio command prompt to enter the DEBUG directory, run the command registration: regasm testcom. dll/TLB: testcom. TLB
Ii. Java calls com
Deploy Jacob
◆ Introduce Jacob. jar in the Development Environment
◆ Copy the jacob-1.15-M3-x86.dll file to the c: \ windows \ system32 directory, if it is a Web application, you also need to copy to the jdk1.5.0 _ 16 \ bin directory (JDK installation directory under the bin directory)
Java call code
Code
Import com. Jacob. ActiveX. activexcomponent;
Import com.jacb.com. comthread;
Import com.jacb.com. Dispatch;
Import com.jacb.com. Variant;
Public class test
{
/*** @ Param ARGs */
Public static void main (string [] ARGs)
{
// Todo auto-generated method stub
Try
{
Activexcomponent dotnetcom = NULL;
Dotnetcom = new activexcomponent ("testcom. encrypt ");
Variant Var = Dispatch. Call (dotnetcom,
"Getencrypt", "Brother is the first parameter", "Brother is the second parameter ");
String STR = var. tostring (); // Return Value
} Catch (exception ex)
{
Ex. printstacktrace ();
}
}
}
This completes the Java method to call. Net DLL!