{"id":348,"date":"2012-10-27T23:53:05","date_gmt":"2012-10-28T04:53:05","guid":{"rendered":"http:\/\/nodakengineering.com\/?page_id=348"},"modified":"2012-11-02T09:20:39","modified_gmt":"2012-11-02T14:20:39","slug":"set-java-path-in-windows","status":"publish","type":"page","link":"https:\/\/nodakengineering.com\/?page_id=348","title":{"rendered":"Set Java Path in Windows"},"content":{"rendered":"<p>I&#8217;ve created a small executable that takes the work out of setting the java environment variable that some people struggle with. It was created using\u00a0<a href=\"http:\/\/bat2exe.net\/\" target=\"_blank\">bat2exe<\/a>\u00a0and makes life simple if you just want Java to work from the command line.<\/p>\n<p>It has been tested and works in XP and Wndows 7 (32 and 64 bit)&#8230;should also work in Windows 2000 and Vista without issues.<\/p>\n<p><strong>NOTE:<\/strong> In Windows 7 &amp; Vista you need to run as Administrator.<\/p>\n<p>You can grab my executable <a href=\"http:\/\/nodakengineering.com\/wp-content\/uploads\/2012\/10\/Java_Path.zip\"><strong>here<\/strong><\/a>.<\/p>\n<p>See the source batch file below:<\/p>\n<pre>@echo off\r\n\r\nif exist \"C:\\Users\\Default\\ntuser.dat\" set OS_ENVIRON=\"win7\"\r\nif exist \"C:\\Documents and Settings\\Default User\\ntuser.dat\" set OS_ENVIRON=\"winxp\"\r\nIF NOT DEFINED OS_ENVIRON goto ERR\r\n\r\nsetlocal ENABLEEXTENSIONS\r\nset VALUE_NAME=CurrentVersion\r\n if exist \"C:\\Program Files (x86)\" goto AMD64\r\n if exist \"C:\\Program Files\" goto x86\r\n goto ERR\r\n\r\n :AMD64\r\n\tset ARCH_TYPE=\"AMD64\"\r\n\tset KEY_NAME=\"HKLM\\SOFTWARE\\Wow6432Node\\JavaSoft\\Java Runtime Environment\"\r\n goto exec\r\n :x86\r\n\tset ARCH_TYPE=\"x86\"\r\n\tset KEY_NAME=\"HKLM\\SOFTWARE\\JavaSoft\\Java Runtime Environment\"\r\n GOTO exec\t\r\n :exec\r\n\r\n::\r\n:: get the current version\r\n::\r\nif %OS_ENVIRON%==\"winxp\" set SKIP_TOKENS=\"usebackq skip=4 tokens=3\"\r\nif %OS_ENVIRON%==\"win7\" set SKIP_TOKENS=\"usebackq skip=2 tokens=3\"\r\n\r\nFOR \/F %SKIP_TOKENS% %%A IN (`REG QUERY %KEY_NAME% \/v %VALUE_NAME% 2^&gt;nul`) DO (\r\n    set ValueValue=%%A\r\n)\r\n\r\nif defined ValueValue (\r\n\r\n    @echo the current Java runtime is  %ValueValue%\r\n) else (\r\n    @echo Java not found...please install and try again!\r\n    goto end\r\n)\r\n\r\nset JAVA_CURRENT=\"%KEY_NAME:~1,-1%\\%ValueValue%\"\r\nset JAVA_HOME=JavaHome\r\n\r\n::\r\n:: get the javahome\r\n::\r\nif %ARCH_TYPE%==\"AMD64\" goto 64Bit\r\nif %ARCH_TYPE%==\"x86\" goto 32Bit\r\ngoto ERR\r\n\r\n:64Bit\r\nFOR \/F \"usebackq skip=2 tokens=3-5\" %%A IN (`REG QUERY %JAVA_CURRENT% \/v %JAVA_HOME% 2^&gt;nul`) DO (\r\n    set JAVA_PATH=%%A %%B %%C\r\n)\r\ngoto path\r\n\r\n:32Bit\r\nif %OS_ENVIRON%==\"winxp\" set SKIP_TOKENS=\"usebackq skip=4 tokens=3,4\"\r\nif %OS_ENVIRON%==\"win7\" set SKIP_TOKENS=\"usebackq skip=2 tokens=3,4\"\r\nFOR \/F %SKIP_TOKENS% %%A IN (`REG QUERY %JAVA_CURRENT% \/v %JAVA_HOME% 2^&gt;nul`) DO (\r\n    set JAVA_PATH=%%A %%B\r\n)\r\ngoto path\r\n\r\n:path\r\nif not exist \"%JAVA_PATH%\" goto PATH_ERR\r\necho The path of the current Java JVM according to the registry is \r\necho %JAVA_PATH%\r\necho.\r\necho Now if we try it:\r\n\"%JAVA_PATH%\\bin\\java.exe\" -version\r\necho.\r\necho Press CTRL-C to abort or any key to add \"%JAVA_PATH%\" to the current path\r\npause\r\necho adding \"%JAVA_PATH%\" to the current path\r\necho adding \"%JAVA_PATH%\\bin\" to the current path\r\nreg add \"HKLM\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment\" \/v Path \/t REG_EXPAND_SZ \/d \"%PATH%;%JAVA_PATH%;%JAVA_PATH%\\bin\" \/f\r\n\r\necho.\r\necho Please reboot for the changes to take effect...\r\npause\r\n\r\n :END\r\ngoto exit\r\n\r\n :PATH_ERR\r\n echo Could not resolve Java path!\r\n pause\r\n goto exit\r\n\r\n :ERR\r\n echo Unsupported architecture!\r\n pause\r\n\r\n:exit<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve created a small executable that takes the work out of setting the java environment variable that some people struggle with. It was created using\u00a0bat2exe\u00a0and makes life simple if you just want Java to work from the command line. It has been tested and works in XP and Wndows 7 (32 and 64 bit)&#8230;should also &#8230; <a title=\"Set Java Path in Windows\" class=\"read-more\" href=\"https:\/\/nodakengineering.com\/?page_id=348\" aria-label=\"Read more about Set Java Path in Windows\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":51,"menu_order":0,"comment_status":"open","ping_status":"open","template":"page-without-sidebar.php","meta":{"footnotes":""},"class_list":["post-348","page","type-page","status-publish"],"_links":{"self":[{"href":"https:\/\/nodakengineering.com\/index.php?rest_route=\/wp\/v2\/pages\/348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nodakengineering.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nodakengineering.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nodakengineering.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nodakengineering.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=348"}],"version-history":[{"count":11,"href":"https:\/\/nodakengineering.com\/index.php?rest_route=\/wp\/v2\/pages\/348\/revisions"}],"predecessor-version":[{"id":369,"href":"https:\/\/nodakengineering.com\/index.php?rest_route=\/wp\/v2\/pages\/348\/revisions\/369"}],"up":[{"embeddable":true,"href":"https:\/\/nodakengineering.com\/index.php?rest_route=\/wp\/v2\/pages\/51"}],"wp:attachment":[{"href":"https:\/\/nodakengineering.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}