java中调用C语言代码

来源:互联网 发布:java反射机制原理 编辑:程序博客网 时间:2024/05/22 15:38

  第一步:搭建C语言开发环境了。

  在此,个人使用的linux系统,介绍的linux的安装,windows的朋友可自行上网搜索。

  linux下内置了编译器gcc,我们要做的只是写好c源文件然后在进行编译就可以了,但是如果没有gcc编译器的话,使用以下命令获取

  ~# sudo apt-get install gcc

  同时要下载辅助工具

  ~# sudo apt-get install binutils

  头文件库

  ~# sudo apt-get install Llibc6-dev

  好了,C语言开发环境已经搭建完毕。

  第二步:写好并编译c源文件

  在此个人给一点小建议:C文件的编写与编译放在java开发项目底进行下,编译完成之后不要再移动编译文件,否则会出现各种各样的问题,比如我这里,一旦移动就必须给编译文件hello.exe在属性中赋予运行权限。比如以下的consider便为我的java项目名。

  在此再给一点小提醒:C文件尽量在哪台机器上使用就在哪台机器上编译,因为不同的机器使用的可能是不同类的处理器和操作系统,如果是另外一台机器编译出来的,放到自己的机器上编译可能就会出现点问题。比如:一般不能先在自己的计算机上编译此程序,再把编译好的内容上载到 Web 服务器。因为自己的计算机可能是widows,而服务器一般为linux。

  在此以编写文件:hello.c为例子:

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12#include <stdio.h>

  #include <stdlib.h>

  #include <string.h>

  int main()

  {

  char buf[1024];

  while(1){

  gets(buf);//标准输入流,输入字符串

  printf("list: %s\n", buf);//标准输入流,将字符串输入

  fflush(stdout); //fflush()用于对输出缓冲区进行刷新。

  }

  }

  写好之后在控制台进入该文件目录,比如:

  ~# cd /home/roadahead/workspace/consider

  接下来编译C源文件:

  ~# gcc hello.c -o hello.exe(hello.c是C源文件,hello.exe为编译后得到的运行文件)

  测试文件是否可用:

  ~# $ ./hello.exe

  hello

  list: hello

  nihao

  list: nihao

  ^C

  出现以上现象证明已经可以使用。

  第三步:编写并运行java

  在此使用eclipse编译工具,项目名:consider,文件名:CHelloWrod.java,上代码:

  1

  2

  3

  4

  5

  6

  7

  8

  9

  10

  11

  12

  13

  14

  15

  16

  17

  18

  19

  20

  21

  22

  23

  24

  25

  26

  27

  28

  29

  30

  31

  32

  33

  34

  35

  36

  37

  38

  39

  40

  41

  42

  43

  44

  45

  46

  47

  48

  49

  50

  51

  52

  53

  54

  55

  56

  57//:CHelloWrod.java

  import java.io.BufferedInputStream;

  import java.io.BufferedOutputStream;

  import java.io.DataInputStream;

  import java.io.File;

  import java.io.IOException;

  import java.io.PrintStream;

  public class CHelloWrod {

  final static int BUFFER_SIZE = 1024;

  public byte[] resultBuf = new byte[BUFFER_SIZE];

  public Process helloP;

  public PrintStream nameList;

  public DataInputStream addResult;

  public CHelloWrod(){

  String CPath = new File("").getAbsolutePath()+"/hello.exe";

  //获取当前项目的路径,hello.exe放在项目根目录下,不管liunx还是widows都适用

  System.out.println(CPath);

  try {sducc1122

  helloP = Runtime.getRuntime().exec(CPath);

  //为启动 C 程序,需要取得当前的 Runtime 对象。我们用它调用exec(),再由后者返回 Process 对象。

  nameList = new PrintStream(new BufferedOutputStream(

  helloP.getOutputStream()));

  //获取C中的标准输入流,封装到java中

  addResult = new DataInputStream(new BufferedInputStream(

  helloP.getInputStream()));

  //获取C中的标准输出流,封装到java中

  } catch (IOException e) {

  System.err.println("Cannot start listmgr.exe");

  System.exit(1);

  http://kashigouwu.com}

  int byteCount;

  for(int i=0; i<10; i++){

  try {

  System.out.println("I output hello"+i);

  nameList.println("hello"+i);

  //向C的标准输入流中输入字符串

  nameList.flush();

  byteCount = addResult.read(resultBuf);

  //从C的标准输出流中得到反馈,存储于resultBuf中

  String result = new String(resultBuf,0,byteCount).trim();

  System.out.println(result);

  } catch (IOException e) {

  System.err.println("Communication error");

  e.printStackTrace();

  }

  try {

  Thread.sleep(1000);

  } catch (InterruptedException e) {

  e.printStackTrace();

  }

  }

  }

  public static void main(String[] args) {

  new CHelloWrod();

  }

  }

  //:---;

  代码意思在代码中已经说得很清楚了,这里就不多累赘,运行得到以下结果:

  /home/roadahead/workspace/consider/hello.exe

  I output hello0

  list: hello0

  I output hello1

  list: hello1

  I output hello2

  list: hello2

  I output hello3

  list: hello3

  I output hello4

  list: hello4

  I output hello5

  list: hello5

  I output hello6

  list: hello6

  I output hello7

  list: hello7

  I output hello8

  list: hello8

  I output hello9

  list: hello9

  证明运行正确,java调用C程序代码已经成功,至于该怎样利用就看个人了。

  各位朋友,如果有什么不对的地方尽管指点,谢谢

原创粉丝点击