Java調用Win API__Java

來源:互聯網
上載者:User

官方網站: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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.