Runtime
ec
ec=Runtime.getRuntime()
ec.exec("这里是应用程序的路径或者命令")
比如:ec.exec("c:\\123.exe")
activeX受限于安全设置,你可以试试Runtime.exec以下是一个简单类的范例,展示了调用本地ls命令的情形:
import java.io.BufferedInputStream
import java.io.IOException
public class ExecLs {
static public void main(String[] args) {
String cmd = "ls"
try {
Process ps = Runtime.getRuntime().exec(cmds)
System.out.print(loadStream(ps.getInputStream()))
System.err.print(loadStream(ps.getErrorStream()))
} catch(IOException ioe) {
ioe.printStackTrace()
}
}
// read an input-stream into a String
static String loadStream(InputStream in) throws IOException {
int ptr = 0
in = new BufferedInputStream(in)
StringBuffer buffer = new StringBuffer()
while( (ptr = in.read()) != -1 ) {
buffer.append((char)ptr)
}
return buffer.toString()
}
}
还可以利用java里面的native关键字。其用于声明本地代码方法。一旦声明,这些方法就能够象你调用其他java方法一样被你的java程序调用。在方法前加上native修饰符,但是不要定义方法的任何实体。如:
public native in meth()
还是用一个简单的调用DLL的例子来说明:
public class NativeDemo {
/**
* @param args
*/
int i
public static void main(String[] args) {
// TODO Auto-generated method stub
NativeDemo ob=new NativeDemo()
ob.i=10
System.out.println("This is ob.i before the native method:"+ob.i)
ob.test()
System.out.println("This is bo.i after the native method:"+ob.i)
}
//申明方法
public native void test()
//指定Dll文件
static{
System.loadLibrary("NativeDemo")
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)