Posted by: lrrp | June 29, 2015

Understanding HelloWorld in Java

When I first started with Java programming, the fist program I wrote was HelloWorld in Java. That time I just copied it from a text book we were referring, I didn’t know anything about it. It took me a lot of time to understand everything about Helloworld in Java and how it works. From that time, I made it a rule to start with writing Helloworld program in any new programming language I learn and try to understand every single element of it in as much detail as possible, but,  to be frank its not that easy.

When you write say Helloworld in Java,  you are not capable enough to understand everything about it, and I found it better that instead of learning everything in one go, you should give yourself some time to let the knowledge you learned sink in. It’s even better if you write Helloworld program multiple times without looking at  the book. I have found that doing it repeatedly helps you to understand it better.

/**
 * Helloworld in Java, your first Java Program
 * 
 * @author Javin Paul
 */
public class HelloWorld {

    public static void main(String args[]) {       
       System.out.println("Helloworld in Java");

    }
}

I am using Eclipse to write it again but I suggest you guys to use notepad and command prompt because that’s where you understand How PATH and CLASSPATH works in Java. So, if you are writing it in notepad then make sure you follow below steps :

1) Save your HelloWorld program into a file also name as HelloWorld.java, name of your public class should exactly match with the name of file.

2) First compile your HelloWorld.java file to create class file, for that just go to the directory where you have saved your HelloWorld.java file and execute following command :

$ javac HelloWorld.java

If you get error “javac is not recognized as an internal or external command” it means your PATH is not set properly and system is not able to look for “javac” command it need to compiler Java file. Its usually due to missing JDK bin directory in PATH and I suggest you to read that article to sort it
out.

3) If you able to compile the Java source file then you will see a class file “HelloWorld.class” file in same directory. It means half job is done, now you just need to run it. In order to run a Java program, you need to use “java” command as shown below :

$ java HelloWorld

Remember there is no extension, its just the name of your public class which contains main method. If everything goes well then you will see message “HelloWorld in Java” in your screen or console. If you get error “Could not find or load main class” then you need to check out that article to find the exact reason. This usually happens when you use package to store your class but in this article, I have not used package so you will very unlikely to get that error.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: