Java调用Win API

官方网站:http://jawinproject.sourceforge.net

把lib文件夹下的jawin.jar和jawin-stubs.jar放到%JAVA_HOME%/jre/lib/ext 目录下 把bin文件夹下的jawin.dll放到%SystemRoot%/System32 目录下

然后就可以运行demo下的Hellodll.java了:

/* * HelloDll.java - * * This file is part of the Jawin Project: http://jawinproject.sourceforge.net/ * * Please consult the LICENSE file in the project root directory, * or at the project site before using this software. */

/* $Id: HelloDll.java,v 1.3 2004/06/14 20:16:38 arosii_moa Exp $ */

package demos;

import org.jawin.COMException; import org.jawin.FuncPtr; import org.jawin.ReturnFlags;

/** * Demo that uses the Win32 MessageBoxW API-method. * * @version $Revision: 1.3 $ * @author Stuart Halloway, http://www.relevancellc.com/halloway/weblog/ */ public class HelloDll {

public static void main(String[] args) throws Exception { FuncPtr msgBox = null; try { msgBox = new FuncPtr("USER32.DLL", "MessageBoxW"); msgBox.invoke_I(0, "Hello From a DLL", "From Jawin", 0, ReturnFlags.CHECK_FALSE); } catch (COMException e) { // handle exception e.printStackTrace(); throw e; } finally { if (msgBox != null) { try { msgBox.close(); } catch (COMException e) { // handle fatal exception e.printStackTrace(); throw e; } } } } }

调用自己写的dll:

import org.jawin.COMException; import org.jawin.FuncPtr; import org.jawin.ReturnFlags;

public class nativeDll { public static void main(String[] args) throws Exception { int result1,result2; FuncPtr test1 = null; FuncPtr test2 = null; try { test1 = new FuncPtr("demo.dll", "test1"); test2 = new FuncPtr("demo.dll", "test2"); result1 = test1.invoke_I(5, 6, ReturnFlags.CHECK_NONE); result2 = test2.invoke_I(5, 6, ReturnFlags.CHECK_NONE); System.out.println("5 plus 6 is " + result1); System.out.println("5 multiply 6 is " + result2); } catch (COMException e) { e.printStackTrace(); } finally { if (test1 != null) { try { test1.close(); } catch (COMException e) { e.printStackTrace(); } } if (test2 != null) { try { test2.close(); } catch (COMException e) { e.printStackTrace(); } } } } }

命令行运行:javac nativeDll.java java -classpath ".;%JAVA_HOME%/jre/lib/ext/jawin.jar" nativeDll

注意,这里一定要加-classpath ".;%JAVA_HOME%/jre/lib/ext/jawin.jar" 不然会提示

Exception in thread "main" java.lang.NoClassDefFoundError: org/jawin/COMExceptio n Caused by: java.lang.ClassNotFoundException: org.jawin.COMException at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source)

特别注意".;",不加的话会提示

Exception in thread "main" java.lang.NoClassDefFoundError: nativeDll Caused by: java.lang.ClassNotFoundException: nativeDll at java.net.URLClassLoader$1.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClassInternal(Unknown Source)

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页