java调用C语言的接口
Java在调用WebService时可以直接使用Apache提供的axis.jar编写自己的代码,也可以使用Eclipse自动生成WebService客户端代码并使用其代理类进行调用。理论上是一样的,只是用Eclipse自动生成代码更容易。
1.代码编写方法:
包com . yudun . test;
导入Java . RMI . remote exception;
导入org . Apache . axis . client . call;
导入org . Apache . axis . client . service;
import org . Apache . axis . message . prefixed qname;
导入org . Apache . axis . message . soapheaderelement;
导入com . Cezanne . golden . user . exception;
import com . Cezanne . golden . user . usermanagerserviceproxy;
导入javax . XML . namespace . qname;
导入Java . net . malformedurexception;
导入javax . XML . RPC . service exception;
导入javax . XML . soap . name;
导入javax . XML . soap . soap exception;
公共类testWebService {
公共静态字符串getResult()引发ServiceException、MalformedURLException、RemoteException、SOAPException
{
//标识Web服务的具体路径。
String endpoint = "WebService服务地址";
//创建服务实例
Service Service = new Service();
//通过服务实例创建Call实例。
Call Call =(Call)service . create Call();
//将Web服务的服务路径添加到调用实例中。
call . settargetendpointaddress(new Java . net . URL(endpoint));//设置调用服务的位置。
//因为需要认证,所以需要设置调用SOAP头信息。
Name Headername = New前缀QName (New QName("发布的wsdl中targetNamespace中的URL "," String _ ItemName "));
org . Apache . axis . message . SOAPHeaderElement header = new SOAPHeaderElement(header name);
header . addtextnode(" blabla bla ");
call . add header(header);
//soapheadlement soapheadlement = new soapheadlement("发布的wsdl中targetNamespace中的URL "," soap header ");
//soapheaderelement . set namespace uri("发布的wsdl中targetNamespace中的URL ");
//尝试
// {
//soapheaderelement . addchildelement(" string _ itemName ")。setValue(" blabla ");
// }
// catch (SOAPException e)
// {
//e . printstacktrace();
// }
//call . add header(soapHeaderElement);
//调用Web服务的方法
org . Apache . axis . description . operation desc oper;
org . Apache . axis . description . parameter desc param;
oper = new org . Apache . axis . description . operation desc();
oper . setname(" op name ");
param = new org . Apache . axis . description . parameter desc(new javax.xml.namespace.QName("、" arg0 ")、org . Apache . axis . description . parameter desc . in、new javax . XML . namespace . qname("、" string ")、java.lang.String.class、false、false);
param . setommittable(true);
oper . add parameter(param);
param = new org . Apache . axis . description . parameter desc(new javax.xml.namespace.QName("、" arg1 ")、org . Apache . axis . description . parameter desc . in、new javax . XML . namespace . qname("、" string ")、java.lang.String.class、false、false);
param . setommittable(true);
oper . add parameter(param);
param = new org . Apache . axis . description . parameter desc(new javax.xml.namespace.QName("、" arg2 ")、org . Apache . axis . description . parameter desc . in、new javax . XML . namespace . qname("、" string ")、java.lang.String.class、false、false);
param . setommittable(true);
oper . add parameter(param);
oper . set returntype(new javax . XML . namespace . qname("," string "));
oper . setreturnclass(Java . lang . string . class);
oper . setreturnqname(new javax . XML . namespace . qname("、" return "));
oper . set style(org . Apache . axis . constants . style . wrapped);
oper . set use(org . Apache . axis . constants . use . literal);
oper . add fault(new org . Apache . axis . description . fault desc(
New javax.xml.namespace.QName("发布的wsdl中targetNamespace中的URL ","异常"),
“例外”,
New javax.xml.namespace.QName("发布的wsdl中targetNamespace中的URL ","异常"),
真实的
));
call . set operation(oper);
call . setoperationname(new javax . XML . namespace . qname("发布的wsdl中targetNamespace中的URL "," opname "));
//调用Web服务,传入参数。
String res = (string)调用。invoke(新对象Java如何调用C函数? 用java调用C语言的库,需要使用Java提供的JNI。\x0d\x0a说明\x0d\x0a在C语言中定义了一个voidsayHello()函数(print hello world);然后用Java调用这个函数来显示HelloWord。\x0d\x0a现在分别从Java和C语言解释:\x0d\x0a1。Java部分\ x0d \ x0a首先定义了一个HelloNative,其中声明了sayHello函数,函数应该声明为Native。具体如下:\ x0d \ x0apublicclasssspecific { \ x0d。\ x0d \ x0a} \ x0d \ x0d \ x0a编译这个类并生成一个类文件:\ x0d \ x0ajavachelloworld.java \ x0d \ x0a \ x0a用javah \ x0d \ x0ajavahhellotive \ x0d生成所需的H文件。x0a生成的h文件大致如下:\ x0d \ x0a \ x0a/* donotedithisfile-itis machine generated */\ x0d \ x0a/* headerforclassshelleinactive */\ x0d \ x0a # ifndef _ Included _ hello active \ x0d \ x0a # define _ Included _ hello active \ x0d \ x0a # ifdef _ _ cplusplus \ x0d \ x0a extern " C " { \ x0d \ x0a # endif \ x0d \ x0d\ x0d \ x0a \ x0d \ x0a # ifdef _ _ cplusplus \ x0d \ x0a } \ x0d \ x0a # Endif \ x0d \ x0a \ x0a \ x0d \ x0a你可以看看上面自动生成的程序,其中包括JNI。\x0d\x0a2。c语言部分\x0d\x0a根据上面生成的H文件编写相应的代码,建立一个HelloNative.cpp实现显示HelloWorld的功能,如下:\ x0d \ x0a \ x0a # include \ x0d \ x0a # include " Hello Native。h" \。x0d \ x0aJNIEXPORTvoidJNICALLJava _ hello active _ say hello(JNI env *,job object)\ x0d \ x0a { \ x0d \ x0a printf(" hello world!\ n ");\x0d\x0a}\x0d\x0a\x0d\x0a代码写出来,然后我们用gcc编译成库文件。该命令如下所示:\ X0d \ x0agcc-fPIC-I/usr/lib/JVM/Java-7-open JDK-i386/include-I/usr/lib/JVM/Java-7-open JDK-i386/include/Linux-shared-olibHelloNative.sohellonative . CPP \ X0d \ x0a \ X0d \ x0a这将在当前目录下生成libhellonative . so的库文件。此时所需的库已经生成,C语言的工作已经完成。\x0d\x0a接下来我们需要用Java写一个程序来测试一下。在程序开始之前,我们需要加载我们的库。加载方法是调用Java的系统。LoadLibrary ("Hello Native!\ x0d \ x0a \ x0d \ x0apublicclassTestNative \ x0d \ x0a { \ x0d \ x0a static { \ x0d \ x0a try { \ x0d \ x0a system . loadlibrary(" hello active ");\ x0d \ x0a } \ x0d \ x0a catch(UnsatisfiedLinkErrore){ \ x0d \ x0a system . out . println(" Cannotloadhellolibrary:\ n " e . tostring());\ x0d \ x0a } \ x0d \ x0a } \ x0d \ x0apublicstaticvoidmain(String[]args){ \ x0d \ x0aHelloNativetest = new hello native();\ x0d \ x0a test . say hello();\ x0d \ x0a } \ x0d \ x0a } \ x0d \ x0a \ x0d \ x0a但是重新编译后,运行时,问题又出现了。\ x0d \ x0acannotloadhellolibrary:\ x0d \ x0ajava . lang . unsatisfiedlinkerror:nohellonativeinjava . library . path \ x0d \ x0a exception inthread " main " Java . lang . unsatisfiedlinkerror:hello active . say hello()V \ x0d \ x0a the lonative . say hello(native method)\ x0d \ x0a attestnative . main(test native . Java:13)\ x0d \ x0d \ x0d \ x0a失败怎么会加载失败呢?\x0d\x0a检查系统。getproperty ("java.library.path ")并发现java.library.path中不存在当前目录,主要有以下几种解决方法:\x0d\x0a1)将生成的库复制到java.library.path的现有路径中,这样不太好\x0d\ X0a2)设置环境变量exportLD_LIBRARY_PATH =。:$ ld _ library _ path,并将当前目录添加到LD _ library _ path \x0d\x0a3)设置java选项,并将当前目录添加到其中。java-djava.library.path =。$ ld _ library _ path \ x0d。从高处或远处观看
java如何调用C语言的接口? 使用Jni
首先,java中的公共类test hello {
静态{
system . loadlibrary(" TestHellos ");
}
public static native void hello(String msg);
public native void get sysid();
public native String get key code(String sysId);
public native boolean test key code(String sysId,String key code);
公共静态void main(String[] args) {
// hello("你好,Kimm!");
test hello t = new test hello();
t.get sysid();
}
}
使用javac TestHello.java,java TestHello,javah-classpath。-详细测试你好。在c中使用HeardFileds中生成的头文件,然后在源文件中实现HeardFileds的方法。实现方法,其实就是你想调用C的方法,
最后更新于 2023-10-11 03:26:51 并被添加「」标签,已有 位童鞋阅读过。
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处
相关文章