Hello World java

To make sure that you have installed your development environment properly, break it in using the Hello World program.

Launch your editor–notepad will do, gedit, TextEdit, TextMate etc. I wouldn’t advise using word processors like MS Word or OpenOffice, it might generate some other characters that will interfere with the compilation process.

Here’s a simple Hello World program.

Get a command line (cmd.exe for Windows, Terminal.app for OSX and xterm | gnome-terminal etc for Linux users). Navigate to the directory where you saved the HelloWorld program, then compile it.

$ javac HelloWorld.java
$ java HelloMain

javac is the Java compiler, if you encountered an error like “command not found” or something similar, that usually means you have a PATH problem, otherwise you should be ready to run your code.

Notice that you when you compile (using javac) you will include the .java extension of the program code, but when running the code (using java) you do not type the .class extension.

Solving the PATH problem

If you encountered the “command not found” error, you can temporarily solve by setting the PATH variable on the current session of the command line.

For windows users

C:\> set PATH=%PATH%;c:\WhereEverYourJavaInstallPathIs\bin;.

Then try the javac command again. For a more persistent way of setting environment variables, you can look at How to set environment variables

For OSX and Linux users

$ export PATH=$PATH:/WhereEverYourJavaInstallPathIs/bin;.

If you want to show appreciation for my efforts dear reader, you could buy me a tall hazel nut Americano ($2) via PayPal. Thanks
Post navigation
(previous post)
(next post)
| | | .

{0 Comments .. you can add one }

Leave a comment