问题描述:
该函数在win平台上面调用不是很成功,gui会可以调出来,但可能和屏蔽调其中的一些用到cui的功能,而调用cui如masm.exe是很不成功,出现一个黑屏,没用内容,或者根本不有结果。
其程序如下:
class runthread{
process t
public void run(){
try{
t = java.lang.runtime.getruntime().exec("c://masm.exe");
}catch(java.io.ioexception e )
{
system.out.println(e);
}
}
}
/************************************************/
class runexenative{
public static void main(string args[]){
runthread rt = new runthread();
rt.run();
}
}
下面是jni + c 的本地化的实现方法:
写一个:callexenative.h
步骤如下:
step one :
/**** callexenative.java ****/
编写callexenative.java文件
import java.lang.*;
class callexenative{
public native static void cen();
static
{
system.loadlibrary("callexenative");
}
}
在命令行里用:
javac callexenative.java
javah callexenative
然后你就多了一个 :callexenative.h
它的文件如下:
/* do not edit this file - it is machine generated */
#include <jni.h>
/* header for class callexenative */
#ifndef _included_callexenative
#define _included_callexenative
#ifdef __cplusplus
extern "c" {
#endif
/*
* class: callexenative
* method: cen
* signature: ()v
*/
jniexport void jnicall java_callexenative_cen
(jnienv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
这个文件不用改。
下面要写一个callexenative.c文件:该函数的功能是将你的cui文件调出来
文件如下:
#include "c:/callexenative.h"/*在你的机器上调试时可能要修改该文件路径*/
#include <stdio.h>
#include <process.h>
#include <conio.h>
jniexport void jnicall java_callexenative_cen(jnienv* env, jclass cl)
{
char prog[80];
printf("hello callexenative!/n");
printf( "enter name of program to exec: " );
gets( prog );
_execl( prog, prog, "_execl", "two", null );
}
现在visual c++ 的cl 功能在命令行下
c:/>cl /ld callexenative.c
之后你 会发现生成了一个callexenative.dll文件,现在写runexenative.java文件:
/*include
callexenative.h
callexenative.dll
*/
import java.lang.*;
/*load a native method : to call a native file*/
class callexenative{
public native static void cen();
static
{
system.loadlibrary("callexenative");
}
}
/************************************************/
class runexenative{
public static void main(string args[]){
callexenative cen1 = new callexenative();
cen1.cen();
}
}
同样用
javac runexenative.java
java runexenative
之后你就会发现提示你输入cui文件的路径
你输入路径即可!
说明:要注意的是callexenative.h和callexenative.dll应该都在同一个文件夹(我的是在c:/jdk1.3.1_11/bin/).
不足之处:破坏了java的移植性!而且你想想看,同样的功能在vb下用shell(filepath,windowstyle)很容易就搞定,你还会用java吗?我写这篇文章的目的是希望大家找到一种更好的方法来实现这个功能。
闽公网安备 35060202000074号