The Artima Developer Community
Sponsored Link

Java Buzz Forum
New Article by Sanjay Dasgupta: Simplify Native Code Access with JNA

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
News Manager

Posts: 47623
Nickname: newsman
Registered: Apr, 2003

News Manager is the force behind the news at Artima.com.
New Article by Sanjay Dasgupta: Simplify Native Code Access with JNA Posted: Nov 12, 2009 5:48 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by News Manager.
Original Post: New Article by Sanjay Dasgupta: Simplify Native Code Access with JNA
Feed Title: Java Today
Feed URL: http://weblogs.java.net/blog/editors/index.rdf
Feed Description: Java Today on java.net
Latest Java Buzz Posts
Latest Java Buzz Posts by News Manager
Latest Posts From Java Today

Advertisement

We've just published Sanjay Dasgupta's new article Simplify Native Code Access with JNA. In this article, Sanjay describes the path through which Java developers can apply Java Native Access to directly interact with native operating system APIs, presenting several working examples that access Microsoft Windows native APIs, and also delving into the complexities of memory mapping and alignment issues.

Sanjay has packaged up his examples into a single zip file that you can download and unpack into a directory. His package includes the jna.jar file, a build.bat file that you execute from within a Windows command window, and individual *.bat files that you run from within a Windows command window to launch the individual applications.

The applications include a "Hello World" app that signals "Hello World" in Morse code using the Windows kernel's Beep() function; a Java implementation of the Windows LockWorkStation() function; a program that brings into Java the Windows list of available logical drives; and an app that applies TWAIN to connect to a scanning or image device.

Sanjay dug deep into the java.net Articles archive as he did the initial research for his article, tapping Jeff Friesen's 2004 java.net article Java Tech: Acquire Images with TWAIN and SANE, Part 1 for some example JNI code that Sanjay modifies to apply JNA in his new article. Kudos to Jeff for writing a hard technology article that continues to have relevance five years later! [Note: Jeff continues to contribute articles to java.net; his most recent is Introducing Custom Paints to JavaFX.]

I may have greater interest in "low-level" issues than many developers. After all, I started out counting bits and bytes passed over RS-232 serial wires and GPIB cables in the late 1970s and early 1980s. I also spent plenty of time and effort working on the type of memory issues that come into play when you try to integrate someone's legacy library written in a language other than the one that your controlling system is written in, where you have to control and manage their software from a different language.

So, the latter portion of Sanjay's article, where he describes and analyzes memory alignment issues that can arise when you attempt to link Java to native Windows API functions via JNA -- I guess I find that discussion both interesting and comforting. Because in that section, Sanjay makes clear the type of not-obvious problems you may have to deal with when you interface what are basically "alien" languages (Java and a native operating system API). Operating systems and their APIs assume things regarding bits and bytes. And different systems and languages, choosing among the possible options, implement things differently.

Memory is fundamental to operating systems and languages. If you have a memory block that is subdivided into various elements, what are the rules for those subdivisions? Should each element start at a "word" boundary? But, then, what's a "word"? The definition can differ for different OSs. Do you pad with unused bytes if one element ends at a memory location that is not a natural boundary? Or do you start the next element in the middle of that memory "word"? But wouldn't it be weird to do that? Well, to some language and OS designers, that seemed weird, to others it didn't (why waste memory?).

The fact that someone had to make these decisions for each OS and language, and the fact that a toss of the coin was sometimes as good a means of making a choice as anything else (since what's "best" really isn't clear), means that communications between "aliens" (like Java and a native OS API) can be fraught with misinterpretation due to differing assumptions about memory boundaries, memory application, and memory alignment.

I really like Sanjay's treatment of these issues! It's one of the clearest presentations I've seen.

If you need to interface Java with APIs from a native OS, for example, Windows -- or if you're just curious about how you'd go about accomplishing this -- take a look at Sanjay Dasgupta's Simplify Native Code Access with JNA. It's an excellent presentation of the relevant issues!


In Java Today, Adam Bien demonstrates How to Pass Context between Layers with threadlocal and EJB 3.(1):

TransactionSynchronizationRegistry is the way to go - you don't have to worry about the existence of multiple thread pools. However it only works inside a transaction. If you need to pass the context from the web layer (before a transaction is initiated), to the EJB container, where the transaction actually starts, you cannot use TransactionSynchronizationRegistry...

Ludovic Poitou writes about the OpenDS Silent install:

One of the things we're the most proud of in the OpenDS project is the simplicity of installation and initial configuration, thanks to the Java Web Start QuickSetup installer. We say that you can download, install and configure OpenDS to run on your machine in less than 3 minutes and 6 clicks...

I received an email about a new webinar titled Java EE 6 Overview:

Java EE 6 improves on developer productivity features, breaks the "one size fits all" approach with Profiles, adds extensibility, and more. This webinar will get you developing with Java EE 6 today and will cover:

  • Java EE evolution
  • Java EE 6 features overview
  • GlassFish v3 features
  • Getting started with Java EE 6

In today's Weblogs, I noticed Arun Gupta's JFall 2009 Trip Report:

Attended and presented at my first NLJUG's JFall conference earlier today. NLJUG is the Java User Group of Netherlands. It's a non-profit organization that try to get out the maximum out of content sharing with all of its members. An annual subscription to the JUG is 35.50 Euros and allows the members to attend its two annual conferences - JSpring and JFall and also provide a year long subscription to a local Java magazine...

Aaron Houston responds to a question in Aaron? Where do you keep photos from JavaOne, DEVOXX, etc?:

People often ask me where are the photos I took from various community events. Go here http://picasaweb.google.com/sunahouston2...

Fabrizio Giudici announces forceTen 0.5.0 released:

forceTen has gotten the first public release and it's now possible to download it both as a binary application and as a project to compile from sources (in addition to the cloning of the source repository). Also, I've set up the mailing lists, a user's forum and the issue tracker. I don't like the graphics of the new web site (it seems I'm suffering from a lack of fantasy for the clip art), but they are a reasonable starter...

In the Forums, ojerrag has a problem with JXta Connecting to a Rendevous Peer with a fix ip from a peer behind Firewl: "I am building a jxts P2p system and since i am new in Jxta , i am having some problems. I Hava Rendezvou_relay Peer which is running with a fixed Ip address. Trying to connect to the Rendevous Peer using Tcp functions with no problem if the Peer..."

btasdemir has a Combobox problem in lwuit blackberry impl....: "Hi, I use thorsten's lwuit bb impl. I create a combobox with default vector constructor. On the form, when I want to change selected item , I got null pointer exception. So I try to use same combobox with String [] items constructor, I got..."

mikezang asks How to let TextArea scrollUp auto?: "I appended a append() to TextArea which can append one line to TextArea. I hope the TextArea will scroll up automatically when contents beyond TextArea height, what can I do? do you have any idea?"


Our current Spotlight is Geertjan Wielenga's post Financial Applications on the NetBeans Platform: "Experian and Société Générale both have invested in the NetBeans Platform, by using it as the basis of one or more of their applications. And these two are not exactly small organizations. Experian is a global leader in consumer and business credit reporting and marketing services and a constituent of the United Kingdom's FTSE 100 index, with revenues in excess of US$4 billion, while Société Générale is France's second-largest bank by market value. How do I know that these organizations are using the NetBeans Platform? By looking in the dev@openide.netbeans.org mailing list..."


The current java.net Poll asks What do you think about the Java Store's recent progress? The poll will run through Thursday.


Our Feature Articles lead off with Sanjay Dasgupta's new in-depth article Simplify Native Code Access with JNA. We're also featuring Eric Siegelberg's Using a Service Delegate to Avoid MVC Controller Bloat, which describes how to maintain separation of concerns and avoid MVC controller bloat through the use of service delegates. And, our latest Java Tech guest column is Marina Kamahele's "Transparent" Panel - Mixing Heavyweight and Lightweight Components.


The latest Java Mobility Podcast is Java Mobile Podcast 90: Augmented Reality: Excerpts from the JavaOne 2009 Augmented Reality session with Kenneth Andersson and Erik Hellman of Sony Ericsson.


Current and upcoming Java Events:

Registered users can submit event listings for the java.net Events Page using our events submission form. All submissions go through an editorial review before being posted to the site.


Archives and Subscriptions: This blog is delivered weekdays as the Java Today RSS feed. Also, once this page is no longer featured as the front page of java.net it will be archived along with other past issues in the java.net Archive.

-- Kevin Farnham
O'Reilly Media

Read: New Article by Sanjay Dasgupta: Simplify Native Code Access with JNA

Topic: Bharat Sanchar Nigam's 3G services in Orissa by year-end Previous Topic   Next Topic Topic: Facebook's `Sperminator' gets girls pregnant in few clicks!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use