Wednesday, June 11, 2008

Hello World


The first ever program written by a newbie is HelloWorld, be it C, C++, Java or any other programming language. Gives a sense of satisfaction to see the output on the screen. But few get there in the first shot. As for me, it took me god damm five freaking hours to understand what was going wrong.

Most of the times, the first ever HelloWorld program is copied by us till the last 'white character'. Seldom any chance of making any mistakes there. The first problem that hits us is:


C:\>javac HelloWorld.java
'javac' is not recognized as an internal or external command,operable program or batch file.C:\>

javac.exe is a file that gets installed on our machine as a part of jdk installation. This executable is required for generating the byte code i.e. essentially the .class file from our .java files. javac.exe is present in the /bin folder. This bin folder should be available in the 'Path' variable for the command prompt to locate javac.exe.

Copy the path till /bin and set it into the 'Path' variable withing the command prompt and then try the 'javac' command. Don't forget to append %PATH% at the end otherwise you will end up messing up the existing path variables in the current window. Sample from my environment:


C:\>set PATH=D:\apps\JDK_1.5.0.11\bin;%PATH%
C:\>javac
Usage: javac where possible options include:

-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath Specify where to find user class files
-cp Specify where to find user class files
-sourcepath Specify where to find input source files
-bootclasspath Override location of bootstrap class files
-extdirs Override location of installed extensions
-endorseddirs Override location of endorsed standards path
-d Specify where to place generated class files
-encoding Specify character encoding used by source files
-source Provide source compatibility with specified release
-target Generate class files for specific VM version
-version Version information
-help Print a synopsis of standard options
-X Print a synopsis of nonstandard options
-J Pass directly to the runtime system
C:\>

Similar problems are observed with the 'jar' command. jar.exe is an executable which helps us in creating java archive (jar), web archive (war) and enterprise archive (ear) files. In case jar -uvf xyz.jar com/* command gives you an error, the solution is same as above.
Many a times I have resources asking me that even if /bin is in the classpath, we get 'javac not recognised command ....' error. JRE is abbreviation for Java Runtime Environment. The bin folder under jre holds set of executables and binaries required by JVM (Java Virtual Machine). Thus development time executables, like javac, jar, javadoc, rmic to name a few, will not be present in this bin folder. Runtime executables like java, rmiregistry etc will be available in jre/bin.
Hope this blog serves as a good appetizer.
Cheers, Amey

No comments: