Java Call Win Api__java

Source: Internet
Author: User

Official website: http://jawinproject.sourceforge.net

Put the Jawin.jar and Jawin-stubs.jar under the Lib folder into the%java_home%/jre/lib/ext directory and put the Jawin.dll under the Bin folder under the%systemroot%/system32 directory.

Then you can run the Hellodll.java under the demo:

* * * Hellodll.java-* * This file is part of the Jawin project:http://jawinproject.sourceforge.net/* * Please consult T He 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 C Lass 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; }     }    } } }

Invoke the DLL you wrote:

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; &nbsp ;  funcptr test1 = null;    funcptr test2 = null;    try {    test1 = new Funcptr ("Demo.dll", "test1");     test2 = new Funcptr ("De Mo.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 {  &nbsp ; if (test1!= null) {     try {      test1.close ();    & nbsp catch (COMException e) {      e.printstacktrace ();     }   & nbsp }   &NBsp if (test2!= null) {     try {      test2.close ();    & nbsp catch (COMException e) {      e.printstacktrace ();     }   & nbsp }   }}}

Command line run: Javac nativedll.java java-classpath ".; %java_home%/jre/lib/ext/jawin.jar "Nativedll

Note that the-classpath must be added here. %java_home%/jre/lib/ext/jawin.jar "or you'll be prompted

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 (unkn Own Source)

Special attention to ".;", without words will prompt

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.