{"id":26952,"date":"2021-05-06T23:10:04","date_gmt":"2021-05-07T03:10:04","guid":{"rendered":"http:\/\/bangla.salearningschool.com\/recent-posts\/exec-java-provides-static-methods-for-running-external-processes-from-applications-programming-code-examples-java-j2ee-j2me-basic-java\/"},"modified":"2021-05-06T23:10:04","modified_gmt":"2021-05-07T03:10:04","slug":"exec-java-provides-static-methods-for-running-external-processes-from-applications-programming-code-examples-java-j2ee-j2me-basic-java","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=26952","title":{"rendered":"Exec.java Provides static methods for running external processes from applications. #Programming Code Examples #Java\/J2EE\/J2ME #Basic Java"},"content":{"rendered":"<pre>\nimport java.io.*;\n\n\/** A class that eases the pain of running external processes\n *  from applications. Lets you run a program three ways:\n *  <ol>\n *     <li><b>exec<\/b>: Execute the command, returning\n *         immediately even if the command is still running.\n *         This would be appropriate for printing a file.\n *     <\/li><li><b>execWait<\/b>: Execute the command, but don?t\n *         return until the command finishes. This would be\n *         appropriate for sequential commands where the first\n *         depends on the second having finished (e.g.,\n *         <code>javac<\/code> followed by <code>java<\/code>).\n *     <\/li><li><b>execPrint<\/b>: Execute the command and print the\n *          output. This would be appropriate for the Unix\n *          command <code>ls<\/code>.\n *  <\/li><\/ol>\n *  Note that the PATH is not taken into account, so you must\n *  specify the <b>full<\/b> pathname to the command, and shell\n *  built-in commands will not work. For instance, on Unix the\n *  above three examples might look like:\n *  <ol>\n *    <li><pre>Exec.exec(&quot;\/usr\/ucb\/lpr Some-File&quot;);<\/pre>\n<p> *    <\/li>\n<li>\n<pre>Exec.execWait(&quot;\/usr\/local\/bin\/javac Foo.java&quot;);\n *        Exec.execWait(&quot;\/usr\/local\/bin\/java Foo&quot;);<\/pre>\n<p> *    <\/li>\n<li>\n<pre>Exec.execPrint(&quot;\/usr\/bin\/ls -al&quot;);<\/pre>\n<p> *  <\/li>\n<\/ol>\n<p> *<br \/>\n *  Taken from Core Web Programming from<br \/>\n *  Prentice Hall and Sun Microsystems Press,<br \/>\n *  .<br \/>\n *  &copy; 2001 Marty Hall and Larry Brown;<br \/>\n *  may be freely used or adapted.<br \/>\n *\/<\/p>\n<p>public class Exec {<\/p>\n<p>  private static boolean verbose = true;<\/p>\n<p>  \/** Determines if the Exec class should print which commands<br \/>\n    * are being executed, and prints error messages if a problem<br \/>\n    * is found. Default is true.<br \/>\n    *<br \/>\n    * @param verboseFlag true: print messages, false: don?t.<br \/>\n    *\/<\/p>\n<p>  public static void setVerbose(boolean verboseFlag) {<br \/>\n    verbose = verboseFlag;<br \/>\n  }<\/p>\n<p>  \/** Will Exec print status messages? *\/<\/p>\n<p>  public static boolean getVerbose() {<br \/>\n    return(verbose);<br \/>\n  }<\/p>\n<p>  \/** Starts a process to execute the command. Returns<br \/>\n    * immediately, even if the new process is still running.<br \/>\n    *<br \/>\n    * @param command The <b>full<\/b> pathname of the command to<br \/>\n    * be executed. No shell built-ins (e.g., &quot;cd&quot;) or shell<br \/>\n    * meta-chars (e.g. &quot;&gt;&quot;) are allowed.<br \/>\n    * @return false if a problem is known to occur, but since<br \/>\n    * this returns immediately, problems aren?t usually found<br \/>\n    * in time. Returns true otherwise.<br \/>\n    *\/<\/p>\n<p>  public static boolean exec(String command) {<br \/>\n    return(exec(command, false, false));<br \/>\n  }<\/p>\n<p>  \/** Starts a process to execute the command. Waits for the<br \/>\n    * process to finish before returning.<br \/>\n    *<br \/>\n    * @param command The <b>full<\/b> pathname of the command to<br \/>\n    * be executed. No shell built-ins or shell metachars are<br \/>\n    * allowed.<br \/>\n    * @return false if a problem is known to occur, either due<br \/>\n    * to an exception or from the subprocess returning a<br \/>\n    * nonzero value. Returns true otherwise.<br \/>\n    *\/<\/p>\n<p>  public static boolean execWait(String command) {<br \/>\n    return(exec(command, false, true));<br \/>\n  }<\/p>\n<p>  \/** Starts a process to execute the command. Prints any output<br \/>\n    * the command produces.<br \/>\n    *<br \/>\n    * @param command The <b>full<\/b> pathname of the command to<br \/>\n    * be executed. No shell built-ins or shell meta-chars are<br \/>\n    * allowed.<br \/>\n    * @return false if a problem is known to occur, either due<br \/>\n    * to an exception or from the subprocess returning a<br \/>\n    * nonzero value. Returns true otherwise.<br \/>\n    *\/<\/p>\n<p>  public static boolean execPrint(String command) {<br \/>\n    return(exec(command, true, false));<br \/>\n  }<\/p>\n<p>  \/** This creates a Process object via Runtime.getRuntime.exec()<br \/>\n    * Depending on the flags, it may call waitFor on the process<br \/>\n    * to avoid continuing until the process terminates, and open<br \/>\n    * an input stream from the process to read the results.<br \/>\n    *\/<\/p>\n<p>  private static boolean exec(String command,<br \/>\n                              boolean printResults,<br \/>\n                              boolean wait) {<br \/>\n    if (verbose) {<br \/>\n      printSeparator();<br \/>\n      System.out.println(&quot;Executing '&quot; + command + &quot;'.&quot;);<br \/>\n    }<br \/>\n    try {<br \/>\n      \/\/ Start running command, returning immediately.<br \/>\n      Process p  = Runtime.getRuntime().exec(command);<\/p>\n<p>      \/\/ Print the output. Since we read until there is no more<br \/>\n      \/\/ input, this causes us to wait until the process is<br \/>\n      \/\/ completed.<br \/>\n      if(printResults) {<br \/>\n        BufferedReader buffer = new BufferedReader(<br \/>\n          new InputStreamReader(p.getInputStream()));<br \/>\n        String s = null;<br \/>\n        try {<br \/>\n          while ((s = buffer.readLine()) != null) {<br \/>\n            System.out.println(&quot;Output: &quot; + s);<br \/>\n          }<br \/>\n          buffer.close();<br \/>\n          if (p.exitValue() != 0) {<br \/>\n            if (verbose) {<br \/>\n              printError(command + &quot; -- p.exitValue() != 0&quot;);<br \/>\n            }<br \/>\n            return(false);<br \/>\n          }<br \/>\n        } catch (Exception e) {<br \/>\n          \/\/ Ignore read errors; they mean the process is done.<br \/>\n        }<\/p>\n<p>      \/\/ If not printing the results, then we should call waitFor<br \/>\n      \/\/ to stop until the process is completed.<br \/>\n      } else if (wait) {<br \/>\n        try {<br \/>\n          System.out.println(&quot; &quot;);<br \/>\n          int returnVal = p.waitFor();<br \/>\n          if (returnVal != 0) {<br \/>\n            if (verbose) {<br \/>\n              printError(command);<br \/>\n            }<br \/>\n            return(false);<br \/>\n          }<br \/>\n        } catch (Exception e) {<br \/>\n          if (verbose) {<br \/>\n            printError(command, e);<br \/>\n          }<br \/>\n          return(false);<br \/>\n        }<br \/>\n      }<br \/>\n    } catch (Exception e) {<br \/>\n      if (verbose) {<br \/>\n        printError(command, e);<br \/>\n      }<br \/>\n      return(false);<br \/>\n    }<br \/>\n    return(true);<br \/>\n  }<\/p>\n<p>  private static void printError(String command,<br \/>\n                                 Exception e) {<br \/>\n    System.out.println(&quot;Error doing exec(&quot; + command + &quot;): &quot; +<br \/>\n                        e.getMessage());<br \/>\n    System.out.println(&quot;Did you specify the full &quot; +<br \/>\n                       &quot;pathname?&quot;);<br \/>\n  }<\/p>\n<p>  private static void printError(String command) {<br \/>\n    System.out.println(&quot;Error executing ?&quot; + command + &quot;?.&quot;);<br \/>\n  }<\/p>\n<p>  private static void printSeparator() {<br \/>\n    System.out.println<br \/>\n      (&quot;==============================================&quot;);<br \/>\n  }<br \/>\n}<\/p>\n<p>Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd, 2017 From: http:\/\/sitestree.com\/?p=10215<br \/> Categories:Programming Code Examples, Java\/J2EE\/J2ME, Basic Java<br \/>Tags:Java\/J2EE\/J2MEBasic Java<br \/> Post Data:2017-01-02 16:04:23<\/p>\n<p>\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com\/' target='new' rel=\"noopener\">https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\t(Big Data, Cloud, Security, Machine Learning): Courses: <a href='http:\/\/Training.SitesTree.com' target='new' rel=\"noopener\"> http:\/\/Training.SitesTree.com<\/a><br \/>\n\t\tIn Bengali: <a href='http:\/\/Bangla.SaLearningSchool.com' target='new' rel=\"noopener\">http:\/\/Bangla.SaLearningSchool.com<\/a><br \/>\n\t\t<a href='http:\/\/SitesTree.com' target='new' rel=\"noopener\">http:\/\/SitesTree.com<\/a><br \/>\n\t\t8112223 Canada Inc.\/JustEtc: <a href='http:\/\/JustEtc.net' target='new' rel=\"noopener\">http:\/\/JustEtc.net (Software\/Web\/Mobile\/Big-Data\/Machine Learning) <\/a><br \/>\n\t\tShop Online: <a href='https:\/\/www.ShopForSoul.com'> https:\/\/www.ShopForSoul.com\/<\/a><br \/>\n\t\tMedium: <a href='https:\/\/medium.com\/@SayedAhmedCanada' target='new' rel=\"noopener\"> https:\/\/medium.com\/@SayedAhmedCanada <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>import java.io.*; \/** A class that eases the pain of running external processes * from applications. Lets you run a program three ways: * * exec: Execute the command, returning * immediately even if the command is still running. * This would be appropriate for printing a file. * execWait: Execute the command, but don?t &hellip; <\/p>\n<p><a class=\"more-link btn\" href=\"http:\/\/bangla.sitestree.com\/?p=26952\">Continue reading<\/a><\/p>\n","protected":false},"author":8,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1917],"tags":[],"class_list":["post-26952","post","type-post","status-publish","format-standard","hentry","category-fromsitestree-com","item-wrap"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":10494,"url":"http:\/\/bangla.sitestree.com\/?p=10494","url_meta":{"origin":26952,"position":0},"title":"Exec.java Provides static methods for running external processes from applications.","author":"","date":"August 29, 2015","format":false,"excerpt":"import java.io.*; \/** A class that eases the pain of running external processes \u00a0*\u00a0 from applications. Lets you run a program three ways: \u00a0* \u00a0 \u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0 \u00a0 \u00a0\u00a0\u00a0 exec: Execute the command, returning \u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 immediately even if the command is still running. \u00a0\u00a0\u00a0\u00a0 *\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 This would be appropriate\u2026","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":22130,"url":"http:\/\/bangla.sitestree.com\/?p=22130","url_meta":{"origin":26952,"position":1},"title":"SCJP: Topics and Resources : will be continued #SCJP","author":"Sayed","date":"March 10, 2021","format":false,"excerpt":"SCJP topics and related resources are provided. I have skimed through the resources at least one time.Garbage Collection Test area:Given a code example, recognize the point at which an object becomes eligible for garbage collection, determine what is and is not guaranteed by the garbage collection system, and recognize the\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":76443,"url":"http:\/\/bangla.sitestree.com\/?p=76443","url_meta":{"origin":26952,"position":2},"title":"Linux: Disk Space, Memory Space, Running Processes","author":"Sayed","date":"December 8, 2024","format":false,"excerpt":"df command \u2013 Shows the amount of disk space used and available on Linux file systems. du command \u2013 Display the amount of disk space used by the specified files and for each subdirectory. free command top or htop command vmstat command dmidecode command \/proc\/meminfo file Linux commands show all\u2026","rel":"","context":"In &quot;Root&quot;","block_context":{"text":"Root","link":"http:\/\/bangla.sitestree.com\/?cat=1"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":26988,"url":"http:\/\/bangla.sitestree.com\/?p=26988","url_meta":{"origin":26952,"position":3},"title":"Application that reports all command-line arguments #Programming Code Examples #Java\/J2EE\/J2ME #Basic Java","author":"Author-Check- Article-or-Video","date":"May 7, 2021","format":false,"excerpt":"****************** ShowArgs.java Application that reports all command-line arguments. ****************** *\/ public class ShowArgs { public static void main(String[] args) { for(int i=0; i<args .length; i++) { System.out.println(\"Arg \" + i + \" is \" + args[i]); } } } \/* Note: Brought from our old site: http:\/\/www.salearningschool.com\/example_codes\/ on Jan 2nd,\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":10523,"url":"http:\/\/bangla.sitestree.com\/?p=10523","url_meta":{"origin":26952,"position":4},"title":"Application that reports all command-line arguments","author":"","date":"August 29, 2015","format":false,"excerpt":"****************** ShowArgs.java Application that reports all command-line arguments. ****************** \u00a0*\/ public class ShowArgs { \u00a0 public static void main(String[] args) { \u00a0\u00a0\u00a0 for(int i=0; i","rel":"","context":"In &quot;Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8&quot;","block_context":{"text":"Code . Programming Samples . \u09aa\u09cd\u09b0\u09cb\u0997\u09cd\u09b0\u09be\u09ae \u0989\u09a6\u09be\u09b9\u09b0\u09a8","link":"http:\/\/bangla.sitestree.com\/?cat=1417"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":74784,"url":"http:\/\/bangla.sitestree.com\/?p=74784","url_meta":{"origin":26952,"position":5},"title":"Run JavaFX Applications","author":"Sayed","date":"June 4, 2022","format":false,"excerpt":"javac --module-path \"c:\\Program Files\\Java\\javafx-sdk-18.0.1\\lib\" --add-modules javafx.controls,javafx.fxml *.java java --module-path \"c:\\Program Files\\Java\\javafx-sdk-18.0.1\\lib\" --add-modules javafx.controls,javafx.fxml Painter Download JavaFX: https:\/\/openjfx.io\/ [may be an ok place to download from: verify yourself. https:\/\/gluonhq.com\/products\/javafx\/] Source\/Oracle: https:\/\/www.oracle.com\/downloads\/ Ref: https:\/\/stackoverflow.com\/questions\/62129569\/how-to-run-javafx-application-from-command-line","rel":"","context":"In &quot;\u09ac\u09cd\u09b2\u0997 \u0964 Blog&quot;","block_context":{"text":"\u09ac\u09cd\u09b2\u0997 \u0964 Blog","link":"http:\/\/bangla.sitestree.com\/?cat=182"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/8"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26952"}],"version-history":[{"count":0,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/26952\/revisions"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26952"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}