高手们请问这个“本地方法”怎么用 为什么我那样写不行啊?先谢了!
Native methods 本地方法:本地方法是指使用依赖平台的语言编写的方法,它用来完成Java无法处理的某些依赖于平台的功能。 我的测试类:
import java.io.IOException;
public class Test { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Process p=null;
String cmdarray[]=new String[]{"ipconfig /all"};
try {
p=execInternal(cmdarray,null,null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private native static Process execInternal(String cmdarray[], String envp[], String path)
throws IOException;}
弹出 Java Virtual Machine Launcher
Fatal exception occurred. Program will exit.
错误:
java.lang.UnsatisfiedLinkError: execInternal
at Test.execInternal(Native Method)
at Test.main(Test.java:15)
Exception in thread "main"
Native methods 本地方法:本地方法是指使用依赖平台的语言编写的方法,它用来完成Java无法处理的某些依赖于平台的功能。 我的测试类:
import java.io.IOException;
public class Test { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Process p=null;
String cmdarray[]=new String[]{"ipconfig /all"};
try {
p=execInternal(cmdarray,null,null);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private native static Process execInternal(String cmdarray[], String envp[], String path)
throws IOException;}
弹出 Java Virtual Machine Launcher
Fatal exception occurred. Program will exit.
错误:
java.lang.UnsatisfiedLinkError: execInternal
at Test.execInternal(Native Method)
at Test.main(Test.java:15)
Exception in thread "main"
要写DLL的
你去下载个 java高级JNI核心理论教程
那个张孝祥的徒弟讲的
JNI这块讲的还不错的不错你的用法 好像是想调用ipconfig.exe????
那这样写
Runtime.getRuntime().exec("ipconfig /all");
Process process = Runtime.getRuntime().exec("ipconfig /all"); Scanner sc = new Scanner(new BufferedInputStream(
process.getInputStream()));
StringBuilder builder = new StringBuilder(); while (sc.hasNext()) {
builder.append(sc.nextLine().trim() + "\n");
} sc.close();这样就可以获得ipconfig /all 的输出, 接着对builder.toString()进行处理
private native Process execInternal(String cmdarray[], String envp[], String path) throws IOException; 这个方法我是从Runtime类里拷出来的,我是想知道为什么放在Runtime 里可以,放在我自己的类里不行?Runtime.getRuntime().exec( "ipconfig /all "); 这个我会用,但不是我的目的,但还是谢谢兄弟的帮忙,呵呵!
你把jre下面的dll都拷到跟class同一目录 试试
http://www.zhanso.com/JAVA/JAVA_1_1808.html
或者看下,下面这个 同样的问题 public static native long currentTimeMillis();这个方法在Date类里拷出来的,为什么放我自己的类里不行???public class TestNative {
public static native long currentTimeMillis();
public static void main(String[] args) {
System.out.print(new TestNative().currentTimeMillis());
}
}
registNatives();
}
private static native void registerNatives();
static {
registerNatives();
}
public static native long currentTimeMillis();
public static void main(String[] args) {
TestNative.currentTimeMillis();
System.out.print("");
}
}java.lang.UnsatisfiedLinkError: registerNatives
at TestNative.registerNatives(Native Method)
at TestNative.<clinit>(TestNative.java:4)
Exception in thread "main"