Accessing Windows applications using the Jawin component in Java

Source: Internet
Author: User
Tags win32

(1) Jawin (JAVA/WIN32) is a free, open source architecture for interoperability between Java components and Windowscom objects or Windowsdll. Jawin allows Java applications to invoke any legacy code that is based on COM or DLL without having to write any JNI code. Your code will be able to interact with scriptable applications such as the MicrosoftOffice Suite, call scripting logical components such as Microsoft's COM-based XML parsers and tools, and access to Windows Registry, Win32API features such as security APIs and event logs. (2) Jawin includes a code generator (typebrowser) for scripting COM components that read the type library and automatically generate Java stubs that are required to invoke the component in a Java application.

http://sourceforge.net/projects/jawinproject/

JAWIN2.0-ALPHA1 Download Address: http://jawinproject.sourceforge.net/

The download file is a zip file called Jawin-2.0-alpha1.zip. After decompression, copy the Bin/jawin.dll file (approximately 264k) to the system's System32 folder and copy Lib/jawin.jar and Lib/jawin-stubs.jar to the c:/j2se6/jre/lib/ Ext folder (my J2SEJDK is installed under the C:/j2se6 folder). Other settings do not need to be done.

For example 1:createword.java, create a new Word document.

/****************************************** *createword.java *createaworddocument * *version1.0 * AuthorHowwa@sina.com http://blog.sina.com.cn/u/1495389370 *******************************************/

IMPORTORG.JAWIN.DISPATCHPTR; Importorg.jawin.win32.Ole32;

public class createword{public static void Main (String[]args) {try{ole32.coinitialize ();//Initialize dispatchptr app=new Dispa Tchptr ("Word.Application"); Creates a Word object App.put ("Visible", true); Make Word visible dispatchptr docs= (dispatchptr) app.get ("Documents"); Gets the Document object collection Dispatchptr doc= (dispatchptr) docs.invoke ("Add"); Add a new document App.invoke ("Activate"); Activates the current document Ole32.couninitialize (); Release Object}catch (Exception e) {e.printstacktrace ();}} }

For example 2:openword.java, open an existing Word document.

/****************************************** *openword.java *openaworddocument * *version1.0 *authorHowwa@sina.com http://blog.sina.com.cn/u/1495389370 *******************************************/

IMPORTORG.JAWIN.DISPATCHPTR; Importorg.jawin.win32.Ole32;

public class openword{public static void Main (String[]args) {try{ole32.coinitialize (); Dispatchptr app=new dispatchptr ("Word.Application"); App.put ("Visible", true); Dispatchptr docs= (dispatchptr) app.get ("Documents"); Dispatchptr doc= (dispatchptr) docs.invoke ("Open", "C://test.doc"); Ole32.couninitialize (); }catch (Exception e) {e.printstacktrace ();}} }

Be aware of the case when compiling and running. At the same time, C packing directory to have test.doc files. /* *createworddoc.java * *1.createanewworddocument; *2.assignfontname *3.assignfontsize *4.writetwosentences *5.saveit *6.closeit *7.exitword. * * @versionvision: 1.0 * @authorhowwa @sina.com *http://blog.sina.com.cn/u/1495389370 * *

IMPORTORG.JAWIN.DISPATCHPTR; Importorg.jawin.win32.Ole32;

Public class createworddoc{public static void Main (String[]args) {try{ole32.coinitialize ();//Initialize Dispatchptr App=ne W dispatchptr ("Word.Application"); Creates a Word object App.put ("Visible", true); Make Word visible dispatchptr docs= (dispatchptr) app.get ("Documents"); Obtain the Document object collection (Documents) dispatchptr doc= (dispatchptr) docs.invoke ("Add"); Add a new document App.invoke ("Activate"); Activates the current document Dispatchptr objtextfont= (dispatchptr) (dispatchptr) doc.get ("Content"). Get ("Font"); Gets the Font object Objtextfont.put ("Name", "bold"); Sets the font objtextfont.put ("Size", "48"); Set Font size dispatchptr docselection= (dispatchptr) app.get ("Selection"); Gets the Selection object Docselection.invoke ("TypeText", "Jawwintesttext!/njawin Test text.") "); Use the TypeText method to add text Doc.invoke ("SaveAs", "C://jawintest.doc"); Save the document (saved in the C-packing directory)//doc.invoke ("Close"); Close the current document, remove the previous annotation and recompile to take effect//app.invoke ("Quit"); Quit Word, remove the previous annotation and recompile to take effect ole32.couninitialize ();//Release Object}catch (Exception e) {e.printstacktrace ();}} }

Related Article

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.