Java IoT Authors: Liz McMillan, Pat Romanski, Elizabeth White, Yeshim Deniz, Zakia Bouachraoui

Related Topics: Java IoT

Java IoT: Article

Invoking JDK Tools at Runtime

Invoking JDK Tools at Runtime

This month's Tips and Tricks column is about how to invoke the JDK command-line tools from within your Java applications and trusted applets. I recently had a chance to actually find this skill useful in a commercial product, so I thought I'd share the technique with you.

There are several command-line-based utility programs that come with the JDK: java', the Java Virtual Machine implementation, jar', for creating and extracting files from Java's new JAR file format, javac' for compiling Java classes and so on. What JavaSoft has done that's pretty cool is added a runtime interface to some of the JDK's command-line based utilities so you can actually call them from within your Java applications and trusted applets.

Here are some potential uses for calling the javac' or jar' tools from within an application or trusted applet:

  • Creating "adapter" objects on-the-fly, which allow you to handle events by exposing a particular interface that's expected by the event source object.
  • One slight annoyance of the serialization API is that you can't "re-start" a serialization file once you've started writing objects to it. Using the jar' tool, you can pack several serialized streams into a single JAR file. This can be useful when communicating over sockets between applications.
  • Use of these tools is almost mandatory when creating JavaBeansª development environments. Case in point: the BeanBox application in the Beans Development Kit.

    This is where I first found out about this new feature of the 1.1 JDK: The 1.1 JDK has a companion development kit specifically for developing JavaBeans, appropriately called the Beans Development Kit (BDK). It's nowhere near as large as the 1.1 JDK, and my best guess tells me that the BDK will be folded into the JDK in the future. The most compelling and useful part of the BDK is a standalone application called the BeanBox. The BeanBox is a test bed for running beans that you've written. At some point while playing with the BeanBox I realized, "Hey! This program is actually creating new Java classes on-the-fly!" (Note that I don't have the space to go into why the BeanBox creates its own Java classes. That's not the issue, anyway.) After I figured out that it was generating its own classes, I asked myself, "How is it doing that?"

    To find the answer, I went where any good Java programmer goes when he or she wants to know how things work, the source code. Buried deep in the BeanBox application (in the HookupManager class, to be exact) is a large section of code that is responsible for creating new Java classes. An abbreviation of similar code is given here as Listing 1.

    Listing 1 shows that an object of class sun.tools.javac.Main is being created and used to actually compile a .JAVA source code file generated by the BeanBox application. So apparently the javac' compiler has been implemented in Java, and there is a programmatic interface to the compiler so we can call it from within other Java applications and trusted applets. Note that we always could invoke the javac' compiler using a System.exec() call, even in the early beta JDK. The nice thing about this programmatic interface to the compiler is that the compiler can now run within the same virtual machine as a running application or trusted applet.

    I have since found that at least two of the other JDK tools also have a similar runtime interface: the java' interpreter program itself and the jar' program for constructing and using Java's new JAR file format. (JAR files are almost the same as ZIP files, which are compressed archives.) I haven't thought of a useful reason for invoking a second JVM within my application or trusted applet yet, which is what invoking the java' application would do. The JAR file format is useful for creating Java archives, however. Listing 2 shows how the jar' utility can be invoked through its Main class from within a Java application or trusted applet.

    Now, there's at least one major caveat I must warn you about when using these sun.tools classes. That is that they are completely file-based. So, as you can see in Listing 1, the BeanBox must write a new .Java file to the local disk and invoke the 'javac' program. The resultant .Class file(s) also are written out to disk automatically, just as if the 'javac' program were being invoked from the command-line. The interface to the jar' application likewise is completely file-based.

    What would be nice is having a completely stream-based interface to these tools. This would allow you to hand the 'javac' compiler an InputStream from which to read a .Java file. The InputStream need not be connected then to the local file system but rather could be a ByteArrayInputStream or some other memory-based character stream. It would also be nice to get the results of the 'javac' compiler, the one or more .Class files it writes, back through another InputStream instead of having to open the file(s) from the local file system. This would allow me to construct new Java classes completely in memory. A similar mechanism when applied to the jar' tool would allow me to stream a compressed file archive to, say, a socket's OutputStream directly.

    One final note: The sun.tools classes are not part of the core Java class libraries. This means that you can't expect these classes to be available within every VM. In addition, distribution of these classes by you is restricted by the JDK licensing agreement (although you can license the pertinent classes from Sun/JavaSoft for inclusion in your applications and/or applets). For general programmers, this means you will only be able to use these classes within the 1.1 JDK's own VM. That is, the applications and applets you create will work only when run within the VM distributed with the 1.1 JDK (a.k.a. the java' interpreter). You must adjust your development plans according to these restrictions.

  • More Stories By Brian Maso

    Brian Maso is President of Blumenfeld & Maso, Inc., a Java and Media consulting company. In addition to his involvement in several commercial Java projects, he is also the author of several Java books, including Osborne/McGraw-Hill's upcoming title "Visual J++ From the Ground Up". Brian is the Java guru of DevelopMentor's Java training courses. He has also written several Java white-papers for individual

    Comments (0)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

    IoT & Smart Cities Stories
    SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
    Founded in 2000, Chetu Inc. is a global provider of customized software development solutions and IT staff augmentation services for software technology providers. By providing clients with unparalleled niche technology expertise and industry experience, Chetu has become the premiere long-term, back-end software development partner for start-ups, SMBs, and Fortune 500 companies. Chetu is headquartered in Plantation, Florida, with thirteen offices throughout the U.S. and abroad.
    SYS-CON Events announced today that DatacenterDynamics has been named “Media Sponsor” of SYS-CON's 18th International Cloud Expo, which will take place on June 7–9, 2016, at the Javits Center in New York City, NY. DatacenterDynamics is a brand of DCD Group, a global B2B media and publishing company that develops products to help senior professionals in the world's most ICT dependent organizations make risk-based infrastructure and capacity decisions.
    DXWorldEXPO LLC announced today that All in Mobile, a mobile app development company from Poland, will exhibit at the 22nd International CloudEXPO | DXWorldEXPO. All In Mobile is a mobile app development company from Poland. Since 2014, they maintain passion for developing mobile applications for enterprises and startups worldwide.
    Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
    Cloud-enabled transformation has evolved from cost saving measure to business innovation strategy -- one that combines the cloud with cognitive capabilities to drive market disruption. Learn how you can achieve the insight and agility you need to gain a competitive advantage. Industry-acclaimed CTO and cloud expert, Shankar Kalyana presents. Only the most exceptional IBMers are appointed with the rare distinction of IBM Fellow, the highest technical honor in the company. Shankar has also receive...
    DXWorldEXPO LLC announced today that ICOHOLDER named "Media Sponsor" of Miami Blockchain Event by FinTechEXPO. ICOHOLDER gives detailed information and help the community to invest in the trusty projects. Miami Blockchain Event by FinTechEXPO has opened its Call for Papers. The two-day event will present 20 top Blockchain experts. All speaking inquiries which covers the following information can be submitted by email to [email protected] Miami Blockchain Event by FinTechEXPOalso offers sp...
    Headquartered in Plainsboro, NJ, Synametrics Technologies has provided IT professionals and computer systems developers since 1997. Based on the success of their initial product offerings (WinSQL and DeltaCopy), the company continues to create and hone innovative products that help its customers get more from their computer applications, databases and infrastructure. To date, over one million users around the world have chosen Synametrics solutions to help power their accelerated business or per...
    DXWordEXPO New York 2018, colocated with CloudEXPO New York 2018 will be held November 11-13, 2018, in New York City and will bring together Cloud Computing, FinTech and Blockchain, Digital Transformation, Big Data, Internet of Things, DevOps, AI, Machine Learning and WebRTC to one location.
    @DevOpsSummit at Cloud Expo, taking place November 12-13 in New York City, NY, is co-located with 22nd international CloudEXPO | first international DXWorldEXPO and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time t...