{"id":2926,"date":"2013-01-30T05:21:04","date_gmt":"2013-01-30T05:21:04","guid":{"rendered":"http:\/\/www.gubatron.com\/blog\/?p=2926"},"modified":"2013-01-30T05:24:58","modified_gmt":"2013-01-30T05:24:58","slug":"how-to-check-if-a-java-process-is-running-on-any-local-virtual-machine-programatically","status":"publish","type":"post","link":"https:\/\/www.gubatron.com\/blog\/how-to-check-if-a-java-process-is-running-on-any-local-virtual-machine-programatically\/","title":{"rendered":"How to check if a Java Process is running on any local Virtual Machine programmatically"},"content":{"rendered":"<p>Quick and dirty way to check if a Java Process is already running.<br \/>\nUseful if you need to run cronjobs periodically and you don&#8217;t know how long they might take, you can add this check at the beginning of your main, and it&#8217;ll look for all the local virtual machines that have a main class named like the &#8220;processName&#8221; string passed to it.<\/p>\n<p>In other words, a quick and dirty programatic jps-like function you can add to your util toolset.<\/p>\n<p>[java]<br \/>\npublic static boolean isJavaProcessRunning(String processName) {<br \/>\n        boolean result = false;<\/p>\n<p>        try {<br \/>\n            HostIdentifier hostIdentifier = new HostIdentifier(&#8220;local:\/\/localhost&#8221;);<br \/>\n            MonitoredHostProvider hostProvider = new MonitoredHostProvider(hostIdentifier);<\/p>\n<p>            MonitoredHost monitoredHost;<br \/>\n            try {<br \/>\n                monitoredHost = MonitoredHost.getMonitoredHost(hostIdentifier);<br \/>\n            } catch (MonitorException e1) {<br \/>\n                e1.printStackTrace();<br \/>\n                return false;<br \/>\n            }<\/p>\n<p>            Set<Integer> activeVms = (Set<Integer>) monitoredHost.activeVms();<br \/>\n            int thisProcessCount = 0;<br \/>\n            for (Integer activeVmId : activeVms) {<br \/>\n                try {<br \/>\n                    VmIdentifier vmIdentifier = new VmIdentifier(&#8220;\/\/&#8221; + String.valueOf(activeVmId) + &#8220;?mode=r&#8221;);<br \/>\n                    MonitoredVm monitoredVm = monitoredHost.getMonitoredVm(vmIdentifier);<br \/>\n                    if (monitoredVm != null) {<br \/>\n                        String mainClass = MonitoredVmUtil.mainClass(monitoredVm, true);<br \/>\n                        if (mainClass.toLowerCase().contains(processName.toLowerCase())) {<br \/>\n                            thisProcessCount++;<br \/>\n                            if (thisProcessCount > 1) {<br \/>\n                                result = true;<br \/>\n                                break;<br \/>\n                            }<br \/>\n                        }<br \/>\n                    }<\/p>\n<p>                } catch (MonitorException e) {<br \/>\n                    e.printStackTrace();<br \/>\n                }<br \/>\n            }<\/p>\n<p>        } catch (URISyntaxException e) {<br \/>\n            e.printStackTrace();<br \/>\n        } catch (MonitorException e) {<br \/>\n            e.printStackTrace();<br \/>\n        }<\/p>\n<p>        return result;<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>Enjoy<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Quick and dirty way to check if a Java Process is already running. Useful if you need to run cronjobs periodically and you don&#8217;t know how long they might take, you can add this check at the beginning of your main, and it&#8217;ll look for all the local virtual machines that have a main class [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[15],"tags":[1437,583,1216,1215,1217],"class_list":["post-2926","post","type-post","status-publish","format-standard","hentry","category-code","tag-code","tag-java","tag-jps","tag-quick-and-dirty","tag-source-code"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5Unzf-Lc","jetpack-related-posts":[{"id":1269,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-a-quick-dirty-hexviewer-updated\/","url_meta":{"origin":2926,"position":0},"title":"How to make a Quick &#038; Dirty HexViewer &#8211; Updated","author":"gubatron","date":"April 20, 2009","format":false,"excerpt":"After I received comments from ispak on Flickr I made a few fixes. ispak pointed out that it was a bad idea reading one byte at the time, also I had a gay ass try\/catch that didn't catch any exception :p So now I read 16 byte chunks, and I\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"HexViewer - r2","src":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/farm4.static.flickr.com\/3486\/3458529439_31357e0afe_o.png?resize=525%2C300 1.5x"},"classes":[]},{"id":980,"url":"https:\/\/www.gubatron.com\/blog\/javareflection-notes-invoking-a-static-main-method-from-a-dinamically-loaded-class\/","url_meta":{"origin":2926,"position":1},"title":"Java\/Reflection notes: Invoking a static main() method from a dinamically loaded class.","author":"gubatron","date":"November 22, 2008","format":false,"excerpt":"Maybe for some wild reason, your Java application will need to execute a pre launcher that won't know about the Main class it's supposed to invoke until it's being executed. For example, you have distributed your Java application but you used pack200 to compress your jars, and your new application\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1252,"url":"https:\/\/www.gubatron.com\/blog\/how-to-make-your-own-quick-dirty-hex-file-viewer-in-java\/","url_meta":{"origin":2926,"position":2},"title":"How to make your own Quick &#038; Dirty Hex File Viewer in Java","author":"gubatron","date":"April 18, 2009","format":false,"excerpt":"Update: You might want to read this new version of the code instead. Thanks ispak I was playing with a hex editor recently and then I thought it would be pretty easy to make a program to output what you see on a text editor. Here's a quick & dirty\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"Same file on HexEdit","src":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3634\/3452718760_995edb5218_o.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/farm4.static.flickr.com\/3634\/3452718760_995edb5218_o.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/farm4.static.flickr.com\/3634\/3452718760_995edb5218_o.png?resize=525%2C300 1.5x"},"classes":[]},{"id":2138,"url":"https:\/\/www.gubatron.com\/blog\/java-how-to-create-dynamic-pngs-jpgs-gifs\/","url_meta":{"origin":2926,"position":3},"title":"Java: How to create dynamic PNGs, JPGs, GIFs.","author":"gubatron","date":"February 14, 2010","format":false,"excerpt":"Sometimes you need to create graphics, or compose images and have them saved as regular PNGs, JPEGs or GIFs. Here's a quick and dirty reference of how to do it with BufferedImages, Graphics2D and javax.imageio.*. Very straightforward. [java] import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO;\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":3661,"url":"https:\/\/www.gubatron.com\/blog\/codejava-scanner-read-a-full-line\/","url_meta":{"origin":2926,"position":4},"title":"[CODE\/JAVA] Scanner &#8211; read a full line","author":"gubatron","date":"January 21, 2017","format":false,"excerpt":"I was solving problems at HackerRank to teach a friend how to code in Java and to process the input data it's convenient to use Java's built in Scanner class. However, in one of the problems we wanted to read a whole line at once and we noticed that the\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2941,"url":"https:\/\/www.gubatron.com\/blog\/java-how-to-get-all-the-files-inside-a-folder-and-its-subfolders-without-recursion\/","url_meta":{"origin":2926,"position":5},"title":"java: How to get all the files inside a folder and its subfolders without recursion","author":"gubatron","date":"March 19, 2013","format":false,"excerpt":"Most programmers will do this in a recursive fashion, but doing that is putting yourself at risk of hitting a stack overflow error, and it's 20% slower (according to my tests). Here's my first implementation of a method that will return just the files (cause I didn't need the folders,\u2026","rel":"","context":"In &quot;Code&quot;","block_context":{"text":"Code","link":"https:\/\/www.gubatron.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/comments?post=2926"}],"version-history":[{"count":3,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2926\/revisions"}],"predecessor-version":[{"id":2929,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/posts\/2926\/revisions\/2929"}],"wp:attachment":[{"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/media?parent=2926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/categories?post=2926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gubatron.com\/blog\/wp-json\/wp\/v2\/tags?post=2926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}