语言混编之c调用java
经过验证可以实现,先将实现代码贴出,以备以后使用。
本文默认你的java开发环境已经安装成功,并且对于java语言和c++语言有过了解。
编写测试用类:Demo.java
代码如下:
public class Demo
{
public static int COUNT = 8;
private String msg;
private int[] counts;
public Demo ()
{
this ("default constructor");
}
public Demo (String msg)
{
this.msg = msg;
this.counts = null;
}
public String getMessage ()
{
return msg;
}
public static String getHelloWorld ()
{
return "hello world!";
}
public String append (String str, int i)
{
return str + i;
}
public int[] getCounts ()
{
return counts;
}
public void setCounts (int[] counts)
{
this.counts = counts;
}
public void throwExcp () throws IllegalAccessException
{
throw new IllegalAccessException ("exception occurs");
}
}
由于在java中能够使用函数重载,所以在c语言中调用java是需要确定究竟是调用的是那个函数。在jdk中提供了javap程序。
Javap将一个类和它的方法的一些转储信息输出到标准输出。该工具不把代码反编译为java源代码,但是它会把字节代码反汇编成为由Java虚拟机规范定义的字节代码指令。其用法如下:
-l 输出本地变量表
-public 输出公用类名和方法名
-protected 输出类的受保护方法
-private 输出类的似有方法
-verbose 输出类的栈的信息和本地方法的个数
回到原来主题:
javap -s -p Demo
无法上传图片^_^想象一下吧。在输出中找到如下的语句:
public java.lang.String append(java.lang.String, int);
Signature: (Ljava/lang/String;I)Ljava/lang/String;
在vs2008中新建一个工程(win32 console application),配置工程属性:将jdk中include目录下的头文件加入到工程中,并将该文夹下的win32子目录下的header file同样加入到工程中。
新建main.cpp
代码如下:
#include
相关文档:
VC中下面几个结构体大小分别是多少呢
struct MyStruct
{
double m4;
char m1;
int m3;
};
struct MyStruct {
  ......
char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char* str5 = "abc";
const char* str6 = "abc";
cout << boolalpha&nb ......
#include <stdio.h>
#include <stdlib.h>
#include <syslog.h>
#include <signal.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#define MAX_READFILE 24
#define MAX_INPUTFILE 10240
char *file ;
void time_out(){
syslog(LOG_INFO,"read inp ......
一位ID为ultimus的程序员开发了一种名为anic的新语言,近日引起业界关注。根据Google Code上该项目的简介,该语言的正式名称是ANI,anic是这种语言的参考实现。
ANI是一种实验性、高性能、静态安全、完全隐含支持并行、面向对象的通用数据流编程语言。
anic用GNU工具链写成,因此可移植性很好,可以运行于所有主流操作系 ......
关于C的思考
Cong Wang
May, 2006
Network Engineering Department
Institute of Post and Telecommunications, Xi'an, P.R.China
引言
C语言结合了汇编的所有威力,它的抽象程度碰巧既满足了程序员的要求, 又容易实现。因其独特的灵活性和强大的可移植性,系统程序员和黑客们更是对它钟爱 ......