{"id":71,"date":"2008-02-28T16:34:29","date_gmt":"2008-02-28T15:34:29","guid":{"rendered":"http:\/\/www.walkingrandomly.com\/?p=71"},"modified":"2008-02-28T16:34:29","modified_gmt":"2008-02-28T15:34:29","slug":"problems-compiling-hello-world-in-java-on-ubuntu","status":"publish","type":"post","link":"https:\/\/walkingrandomly.com\/?p=71","title":{"rendered":"Problems compiling &#8220;Hello World&#8221; in java on ubuntu"},"content":{"rendered":"<p>At some point in the future I am going to need to do some Java programing for work-related purposes and so I thought I should set up my Ubuntu based laptop for Java development.  Everything you need to do this are in the Ubuntu repositories so the installation went without incident but I hit a brick wall when I tried to compile and run this very simple program<\/p>\n<pre>class myfirstjavaprog\r\n{\r\n   public static void main(String args[])\r\n   {\r\n      System.out.println(\"Hello World\");\r\n   }\r\n}<\/pre>\n<p>I compiled this with the command<\/p>\n<p><strong>javac myfirstjavaprog.java<\/strong><\/p>\n<p>The .class file was produced without any problems but when I tried to run it with<\/p>\n<p><strong>java myfirstjavaprog<\/strong><\/p>\n<p>I got a hideous looking error message that started off like this<\/p>\n<p>exception in thread &#8220;main&#8221; java.lang.UnsupportedClassVersionError: Bad version number in .class file<br \/>\nat java.lang.ClassLoader.defineClass1(Native Method)<br \/>\nat java.lang.ClassLoader.defineClass(ClassLoader.java:620)<br \/>\nat java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)<\/p>\n<p>All that for a Hello World program!  This was not a good start but it turns out that there is an easy fix.  The version of Java being used by my java command was different from that being used by my javac command and they need to be the same.  The commands that allow you to configure Java versions on Ubuntu are<\/p>\n<p><strong>sudo update-alternatives &#8211;config java<\/strong><\/p>\n<p><strong>sudo update-alternatives &#8211;config javac<\/strong><\/p>\n<p>Ensure that they are set to use the same version and you should be good to go.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>At some point in the future I am going to need to do some Java programing for work-related purposes and so I thought I should set up my Ubuntu based laptop for Java development. Everything you need to do this are in the Ubuntu repositories so the installation went without incident but I hit a [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":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":[14,7],"tags":[],"class_list":["post-71","post","type-post","status-publish","format-standard","hentry","category-java","category-programming"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3swhs-19","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/71","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=71"}],"version-history":[{"count":0,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=\/wp\/v2\/posts\/71\/revisions"}],"wp:attachment":[{"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=71"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=71"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/walkingrandomly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=71"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}