1 /*
2 * $Id: SystemCallExample.java 106 2008-08-30 00:58:59Z jyeary $
3 *
4 * Software License Agreement (BSD License)
5 *
6 * Copyright (c) 2008, Blue Lotus Software, LLC
7 * All rights reserved.
8 *
9 * Redistribution and use of this software in source and binary forms, with or
10 * without modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * Redistributions of source code must retain the above
14 * copyright notice, this list of conditions and the
15 * following disclaimer.
16 *
17 * Redistributions in binary form must reproduce the above
18 * copyright notice, this list of conditions and the
19 * following disclaimer in the documentation and/or other
20 * materials provided with the distribution.
21 *
22 * Neither the name of Blue Lotus Software, LLC nor the names of its
23 * contributors may be used to endorse or promote products
24 * derived from this software without specific prior
25 * written permission of Blue Lotus Software, LLC
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
28 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
29 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
30 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
33 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36 package com.bluelotussoftware.examples;
37
38 import java.io.BufferedInputStream;
39 import java.io.BufferedReader;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.io.InputStreamReader;
43
44 /**
45 * This is a demo application on how to use Java to make system calls.
46 * The use of system calls is platform specific. This code may not be
47 * transportable accross platforms,i.e. UNIX, Mac OS X, and Windows.
48 * @author John Yeary
49 * @version 1.0
50 */
51 public class SystemCallExample {
52
53 /**
54 * Creates a new instance of SystemCallExample
55 */
56 public SystemCallExample() {
57 }
58
59 /**
60 * Main program method
61 * @param args the command line arguments
62 */
63 public static void main(String[] args) {
64 Runtime r = Runtime.getRuntime();
65
66 try {
67 /*
68 * Here we are executing the UNIX command ls for directory listing.
69 * The format returned is the long format which includes file
70 * information and permissions.
71 */
72 Process p = r.exec("ls -l");
73 InputStream in = p.getInputStream();
74 BufferedInputStream buf = new BufferedInputStream(in);
75 InputStreamReader inread = new InputStreamReader(buf);
76 BufferedReader bufferedreader = new BufferedReader(inread);
77
78 // Read the ls output
79 String line;
80 while ((line = bufferedreader.readLine()) != null) {
81 System.out.println(line);
82 }
83 // Check for ls failure
84 try {
85 if (p.waitFor() != 0) {
86 System.err.println("exit value = " + p.exitValue());
87 }
88 } catch (InterruptedException e) {
89 System.err.println(e);
90 } finally {
91 // Close the InputStream
92 bufferedreader.close();
93 inread.close();
94 buf.close();
95 in.close();
96 }
97 } catch (IOException e) {
98 System.err.println(e.getMessage());
99 }
100 }
101 }
102
Friday, August 29, 2008
Making a System Call in Java
This is an example on how to make a system call to the local operating system to execute external programs. This example was written to work on Mac OS X, but will work on other UNIX type operating systems likely without issue. It will not work as written on Windows unless Cygwin is installed and in the PATH.
Labels:
example
,
Java
,
Netbeans
,
Programming
,
tutorial
Subscribe to:
Post Comments
(
Atom
)
Popular Posts
-
Introduction This article is not another diatribe to tell you the importance of unit testing. I think we can all agree that it is important...
-
A friend of mine asked me if there was a list of reserved words in EL and JSF. He had previously looked for it, and after some Google search...
-
I saw a question posed on stackoverflow called Trouble with Primefaces 3.0.M2 SelectOneMenu Ajax behavior and I had just done an example a...
-
I was working on a couple of SSL based issues when I made a couple of observations. The default self-signed key generation in Java does not ...
-
This is an example on how to make a system call to the local operating system to execute external programs. This example was written to work...
-
We have been doing a lot of work lately with PrimeFaces. A common set of questions comes up about displaying <p:dialog/> boxes on a pa...
-
I was asked earlier today how to reset fields in a JSF application, if the validation fails. In his case, he had a Richfaces table which had...
-
Image by quasarkitten via Flickr The basics for creating a Maven archetype can be found in the Maven - Guide to Creating Archetypes . The ...
-
Previously, I posted an example of how to use JSF 1.2 with form based authentication (j_security_check). In this example, I use JSF 2.x to...
-
Abstract A common use case is to iterate over a collection of elements, and display them on a page. In the world of JSP, we would use a Ja...
5 comments :
This is brilliant. Just what I was looking for. Thanks!! - Arunabh Das
Thank you for the quality of the code.
Nice code, congrats.
Just what I needed. Thanks,
very nice.. I'm just curious as to why you pass the InputStream to a BufferedInputStream and not just pass it directly to the reader?
Thanks..
Better style to pass an InputStream to a BufferedInputStream. It is more efficient with longer streams.
Post a Comment