当前位置: 技术问答>java相关
Help!BCB中如何调用一个Java Class且得到其返回值?
来源: 互联网 发布时间:2015-04-18
本文导语: 求助各位BCB和Java高手。因为和友军共同做一个项目,他们用Java,俺们用BCB(变态吧!)。如果完全是模块化面向对象,一点问题都没有,可是因为这个世界有人的存在,那就要扯皮,所以扯到现在,就要我们的BCB调用...
求助各位BCB和Java高手。因为和友军共同做一个项目,他们用Java,俺们用BCB(变态吧!)。如果完全是模块化面向对象,一点问题都没有,可是因为这个世界有人的存在,那就要扯皮,所以扯到现在,就要我们的BCB调用他们的一个Java Class且得到其返回值。因为,我们在重写一个Class已经来不及了,而且......tmd,反正老板就是要我三天内搞定这个问题。
各位大虾救命呀!!!!!!
各位大虾救命呀!!!!!!
|
发过去了(差点把这事给忘了)
|
我也不懂,但我在java 核心2中找到一些代码贴给你
/**
* @version 1.10 1997-07-01
* @author Cay Horstmann
这是printf3.c文件
*/
#include "Printf3.h"
#include
#include
#include
char* find_format(const char format[])
/**
* @param format a string containing a printf format specifier
* (such as "%8.2f"). Substrings "%%" are skipped.
* @return a pointer to the format specifier (skipping the '%')
* or NULL if there wasn't a unique format specifier
*/
{ char* p;
char* q;
p = strchr(format, '%');
while (p != NULL && *(p + 1) == '%') /* skip %% */
p = strchr(p + 2, '%');
if (p == NULL) return NULL;
/* now check that % is unique */
p++;
q = strchr(p, '%');
while (q != NULL && *(q + 1) == '%') /* skip %% */
q = strchr(q + 2, '%');
if (q != NULL) return NULL; /* % not unique */
q = p + strspn(p, " -0+#"); /* skip past flags */
q += strspn(q, "0123456789"); /* skip past field width */
if (*q == '.') { q++; q += strspn(q, "0123456789"); }
/* skip past precision */
if (strchr("eEfFgG", *q) == NULL) return NULL;
/* not a floating point format */
return p;
}
JNIEXPORT void JNICALL Java_Printf3_fprint
(JNIEnv* env, jclass cl, jobject out, jstring format,
jdouble x)
{ const char* cformat;
char* fmt;
jstring str;
jclass class_PrintWriter;
jmethodID id_print;
cformat = (*env)->GetStringUTFChars(env, format, NULL);
fmt = find_format(cformat);
if (fmt == NULL)
str = format;
else
{ char* cstr;
int width = atoi(fmt);
if (width == 0) width = DBL_DIG + 10;
cstr = (char*)malloc(strlen(cformat) + width);
sprintf(cstr, cformat, x);
str = (*env)->NewStringUTF(env, cstr);
free(cstr);
}
(*env)->ReleaseStringUTFChars(env, format, cformat);
/* now call ps.print(str) */
/* get the class */
class_PrintWriter = (*env)->GetObjectClass(env, out);
/* get the method ID */
id_print = (*env)->GetMethodID(env, class_PrintWriter,
"print", "(Ljava/lang/String;)V");
/* call the method */
(*env)->CallVoidMethod(env, out, id_print, str);
}
以下是printf3.h文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class Printf3 */
#ifndef _Included_Printf3
#define _Included_Printf3
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Printf3
* Method: fprint
* Signature: (Ljava/io/PrintWriter;Ljava/lang/String;D)V
*/
JNIEXPORT void JNICALL Java_Printf3_fprint
(JNIEnv *, jclass, jobject, jstring, jdouble);
#ifdef __cplusplus
}
#endif
#endif
以下是两个java文件
Print3.java
/**
* @version 1.10 1997-07-01
* @author Cay Horstmann
*/
import java.io.*;
class Printf3
{ public static native void fprint(PrintWriter out,
String format, double x);
static
{ System.loadLibrary("Printf3");
}
}
Print3Test.java
/**
* @version 1.10 1997-07-01
* @author Cay Horstmann
*/
import java.io.*;
class Printf3Test
{ public static void main(String[] args)
{ double price = 44.95;
double tax = 7.75;
double amountDue = price * (1 + tax / 100);
PrintWriter out = new PrintWriter(System.out);
Printf3.fprint(out,
"Amount due = %8.2fn", amountDue);
out.flush();
}
}
/**
* @version 1.10 1997-07-01
* @author Cay Horstmann
这是printf3.c文件
*/
#include "Printf3.h"
#include
#include
#include
char* find_format(const char format[])
/**
* @param format a string containing a printf format specifier
* (such as "%8.2f"). Substrings "%%" are skipped.
* @return a pointer to the format specifier (skipping the '%')
* or NULL if there wasn't a unique format specifier
*/
{ char* p;
char* q;
p = strchr(format, '%');
while (p != NULL && *(p + 1) == '%') /* skip %% */
p = strchr(p + 2, '%');
if (p == NULL) return NULL;
/* now check that % is unique */
p++;
q = strchr(p, '%');
while (q != NULL && *(q + 1) == '%') /* skip %% */
q = strchr(q + 2, '%');
if (q != NULL) return NULL; /* % not unique */
q = p + strspn(p, " -0+#"); /* skip past flags */
q += strspn(q, "0123456789"); /* skip past field width */
if (*q == '.') { q++; q += strspn(q, "0123456789"); }
/* skip past precision */
if (strchr("eEfFgG", *q) == NULL) return NULL;
/* not a floating point format */
return p;
}
JNIEXPORT void JNICALL Java_Printf3_fprint
(JNIEnv* env, jclass cl, jobject out, jstring format,
jdouble x)
{ const char* cformat;
char* fmt;
jstring str;
jclass class_PrintWriter;
jmethodID id_print;
cformat = (*env)->GetStringUTFChars(env, format, NULL);
fmt = find_format(cformat);
if (fmt == NULL)
str = format;
else
{ char* cstr;
int width = atoi(fmt);
if (width == 0) width = DBL_DIG + 10;
cstr = (char*)malloc(strlen(cformat) + width);
sprintf(cstr, cformat, x);
str = (*env)->NewStringUTF(env, cstr);
free(cstr);
}
(*env)->ReleaseStringUTFChars(env, format, cformat);
/* now call ps.print(str) */
/* get the class */
class_PrintWriter = (*env)->GetObjectClass(env, out);
/* get the method ID */
id_print = (*env)->GetMethodID(env, class_PrintWriter,
"print", "(Ljava/lang/String;)V");
/* call the method */
(*env)->CallVoidMethod(env, out, id_print, str);
}
以下是printf3.h文件
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class Printf3 */
#ifndef _Included_Printf3
#define _Included_Printf3
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Printf3
* Method: fprint
* Signature: (Ljava/io/PrintWriter;Ljava/lang/String;D)V
*/
JNIEXPORT void JNICALL Java_Printf3_fprint
(JNIEnv *, jclass, jobject, jstring, jdouble);
#ifdef __cplusplus
}
#endif
#endif
以下是两个java文件
Print3.java
/**
* @version 1.10 1997-07-01
* @author Cay Horstmann
*/
import java.io.*;
class Printf3
{ public static native void fprint(PrintWriter out,
String format, double x);
static
{ System.loadLibrary("Printf3");
}
}
Print3Test.java
/**
* @version 1.10 1997-07-01
* @author Cay Horstmann
*/
import java.io.*;
class Printf3Test
{ public static void main(String[] args)
{ double price = 44.95;
double tax = 7.75;
double amountDue = price * (1 + tax / 100);
PrintWriter out = new PrintWriter(System.out);
Printf3.fprint(out,
"Amount due = %8.2fn", amountDue);
out.flush();
}
}
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。