You have to log in to edit pages.
Fill the table below with the information required. It is important to write correctly the keywords and the categories, so that your article will be easy to find to the other users. We also recommend to click on watch this page so you will be notified every time another user modifies your tutorial page.
Click on the help buttons close to each field if you need help in filling them.
Java Advanced Java Basics Java EE Java SE Input Output Ajax Algorithms Application Servers Cryptography Databases Hibernate Debugging J2EE Debugging Design Patterns EJB Exceptions GUI Swing Loops Multithreading Networking Reflection Spring SQL Struts WebServices XDoclet XML XSLT XPath Methods Files Generics Servlets Collections Best practices Java5
Category : Java Advanced Java Basics Java EE Java SE Input Output Ajax Algorithms Application Servers Cryptography Databases Hibernate Debugging J2EE Debugging Design Patterns EJB Exceptions GUI Swing Loops Multithreading Networking Reflection Spring SQL Struts WebServices XDoclet XML XSLT XPath Methods Files Generics Servlets Collections Best practices Java5
Free text: This example shows how to execute a shell command in Java and how to parse the output of the program. In this program I'll show you how to call the '''ping''' command. Basically calling any program should be the same story. == the article == {{versioned code|url=https://svn2.hosted-projects.com/wikijava/articles/dongiulio/SVN_LOCAL/org/wikijava/networking/ping/PingAndComplain.java}} I did this little program one day in which I had very bad internet connection in the office. The connection was down every now and then, leaving me waiting for long times, eventually just to see the timeout page. The program continuously pings a server which is on the net, and if the ping fails for a certain number of times, it uses the [[play wave sound]] to play a sound to advise me that the internet connection went down. The two key classes for this tutorial are <tt>java.lang.Process</tt> and <tt>java.lang.Runtime</tt>. The '''runtime''' class allows the application to interface with the environment in which the application is running. Using the method: <source lang="java"> Process java.lang.Runtime.exec(String command) throws IOException </source> we can execute a command and obtain a <tt>java.lang.Process</tt> from it. At this point the '''command''' is running, we can get the output of it by simply attaching an <tt>java.io.BufferedReader.BufferedReader</tt> to the <tt>inputStream</tt> that we can obtain from the <tt>Process</tt>: <source lang="java"> in = new BufferedReader(new InputStreamReader(process .getInputStream())); </source> At the end To close the process we can call: <source lang="java"> process.destroy(); </source> Calling the <tt>destroy</tt> method should really be a 'just to be sure' procedure. The command should be already terminated when you call <tt>destroy</tt>. Forcing programs to be closed is never a good idea. In ''Ping and Complain'' for instance we call the destroy method only after the process has stopped generating output. Which in general could be considered a good indicator that the command is done. == Code for executing the external program == This section contains a partial version of the program, download the complete source code from the SVN repository at: https://svn2.hosted-projects.com/wikijava/articles/dongiulio/SVN_LOCAL/org/wikijava/networking/ping/PingAndComplain.java like explained in the box above <source lang="java"> //The command to execute String pingCmd = "ping " + ip + " -t"; //get the runtime to execute the command Runtime runtime = Runtime.getRuntime(); Process process = runtime.exec(pingCmd); //Gets the inputstream to read the output of the command BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream())); //reads the outputs String inputLine = in.readLine(); int i = 0; int consecutiveFailures = 0; while ((inputLine != null)) { if (inputLine.length() > 0) { ........ } inputLine = in.readLine(); } </source> == playing the audio file == <source lang="java"> InputStream audioInputStream; try { audioInputStream = new FileInputStream(audioFileName); } catch (FileNotFoundException e1) { .... } PlaySound playSound = new PlaySound(audioInputStream); playSound.play(); </source> == See Also == *[[Play a wave sound in Java]]
Please note that all contributions to WikiJava are considered to be released under the GNU Free Documentation License 1.2 (see Project:Copyrights for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here. You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Summary:
This is a minor edit Watch this page
Cancel