Click here to close now.


Java IoT Authors: Liz McMillan, Pat Romanski, Elizabeth White, Stephen Baker, Victoria Livschitz

Related Topics: Java IoT

Java IoT: Article

Your First Java Program

Lesson 1: Hello World

Getting Started

The Java Development Kit (JDK) could be downloaded from the Sun Microsystems' Internet site at .

The installation process is pretty simple - just run the downloaded executable file and it'll install it on your disk (the default directory for Java under Microsoft Windows is  c:\j2sdk1.4).

To start writing a Java program you could use any plain text editor. In Windows, it could be an editor called Notepad. In UNIX, it could be the vi editor. The files with Java programs must be saved in a plain text format and must have names ending in .java.  For example, if you want to write a program called HelloWorld, enter its code in Notepad and save it in a class named

Keep in mind that Java is a case sensitive language, which means that if you named the program HelloWorld with a capital H and a capital W, do not try to start the program helloworld.

Here is the infamous program that prints the words Hello World on the screen:

public class  HelloWorld {
     public static void main(String[] args){
            System.out.println("Hello World");

Now you need to compile this program. We'll be using the  javac compiler, which is a part of JDK.
Let's say you've saved your program in the directory called  c:\practice. Open a command window, change the current directory to c:\practice and compile the program:

c:\>cd \practice


If your environment is set properly and your program does not have syntax errors, it will create a new file called HelloWorld.class in the same directory.

If an error message is displayed  saying something  like "javac  is not found", or "bad command/file name" make sure that the directory  c:\j2sdk1.4\bin  is  included to the  search path of your environment.    

- If you are using Windows 98, open the file c:\autoexec.bat
        and add  the directory where your JDK is installed to the environment
        variable PATH, for example 

-  In Windows 2000 or XP set the PATH using the menu Settings |
         Control  Panel | System | Environment Variables. 

- In Unix - add it to the shell's PATH environment variable.

You  won't see any confirmation of a successful compilation, just type dir in Windows or ls in Unix, and a new file named HelloWorld.class has to be there. This  proves that your program has been successfully compiled.

If the program has some syntax errors, the compiler will print error messages. In this case you'd need to fix the errors, and recompile the program again. You may need to do it more than once until the file HelloWorld.class is created.

Now let's run the program -  enter the following command:

c:\practice> java HelloWorld

Please note that we do not start  javac, but java , which is called the Java run-time environment or the Java Virtual Machine (JVM).

This time the error message may say that the HelloWorld.class is not found.   Even though you  have the .class file in the same directory as your .java file, JVM is not going to look for it in the current directory unless the current directory is listed in the so-called CLASSPATH variable. Don't confuse this with the variable  PATH, that's been discussed  earlier. 

The variable  CLASSPATH variable is used by the JVM to find compiled classes.  Let's do a procedure similar to what you've done with the PATH.

For example, in Windows 98, open the file autoexec.bat and add the following line to it:


The dot above represents the current directory. If you already had the CLASSPATH variables set in your machine, just add the dot and semicolon to the end of its value.

Give  your Java class and its file the same name.  There could be exceptions to this rule, but not in this simple program.

While writing Java programs, you create classes which represent objects from real life. You'll learn more about classes in the lesson called "Introduction to Object-Oriented Programming in Java".

Our HelloWorld program is also a class and it contains a  method main(). Methods in Java classes represent actions that the class could perform.  The method main() calls the method println() to display the text "Hello World" on the screen.

Here is the method signature of the method main():

public static void main(String[] args)

The method signature includes the access level - public, instructions on usage - static, return value type - void, name of the method - main, and the argument list -  String[] args.

The keyword public means that the method main() could be accessed by any other Java class. The keyword static means that you don't have to create an instance of  this class to use this method. The keyword void says that the method main() doesn't return any value to the calling program.

The keyword Stirng[] args  tells us that this method will receive an array of Strings as the argument (some values could be passed to this method from a command line).

The main() method is the starting point of your program. You can have a program that consists of more that one class, but at least one of them usually has the method main(), otherwise the program will not start. A Java class can have more than one method. For example, a class Employee can have the methods  updateAddress(), raiseSalary(),changeName(), etc.

The body of the method  main()contains the following  line :

System.out.println("Hello World");

The println() is a method that is used to print data on the system console (command window). Java's method names are always followed by parentheses.

System and out are not methods, but names that represent other Java classes.

System.out means that the  variable out is defined inside the class System.

The out.println() tells us that there is an object represented by a variable called  out  and it has  a method called println().

We will be using this so-called dot notation to access class methods or variables. Say you have a class Employee that has a method changeAddress().  Here is an example:

Employee.changeAddress("25 Broadway")

Let's review the steps you would perform to create and run the HelloWorld program:

Step 1. Set the  values for the PATH and CLASSPATH system variables.

Step 2. Create a new directory called practice.

Step 3. Using a text editor, enter the code of the class 
             HelloWorld  and save it in the file  

Step 4.  Compile and run the program:
             c:\practice> javac
             c:\practice> java HelloWorld

Assignment. Write a program to print your address using  more than one statement println().

More Stories By Yakov Fain

Yakov Fain is a co-founder of two software companies: Farata Systems and SuranceBay. He authored several technical books and lots of articles on software development. Yakov is Java Champion ( He leads leads Princeton Java Users Group. Two of Yakov's books will go in print this year: "Enterprise Web Development" (O'Reilly) and "Java For Kids" (No Starch Press).

Comments (4) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
Peter Sweet 04/23/04 11:53:44 AM EDT

This is good stuff. The explanation of what PATH and CLASSPATH do was useful.

Andrey Postoyanets 11/19/03 11:42:21 AM EST

As a Java programmer in a college IT department, I''ve worked with quite a few college interns (and also have been approached by fellow co-workers who want to try Java.) They would usually take a college-level Java course or try self-study, but are quite scared to start on their own. Thus, the usual question is "What should I do first?" Many of them expect some magic IDE and are quite surprised with an answer that a pure Notepad would be enough :)
This lesson (and the series as a whole) would provide an invaluable help to the students and guide them step-by-step into the Java world. It contains examples which are easy to follow and understand. Such useful approach can make more people say "Hey, I can do this! Let me try further..." and attract new developers into our community.

Yakov FaAin 11/18/03 04:25:54 PM EST

Hi J.D,

I agree with you, object-oriented nature is important. But if I start with explaining OOP to people with different backgrounds, I''ll lose a half of my students right away. Guess what? I usually do this on the lesson #2 :)

Yakov Fain

J.D. Baker 11/18/03 11:53:06 AM EST

What''s here is ok, but it ignores the object-oriented nature of the Java programming language. IMHO, the proper approach to Hello World in Java is a version published by Shengyang Shong (I forget where). This version of Hello World had a class with a main called SayHello that instantiated a Mouth that had a say() method that printed "hello". This example can be expanded, introducing constructors for the Mouth and overloaded say() methods.


@ThingsExpo Stories
“The Internet of Things transforms the way organizations leverage machine data and gain insights from it,” noted Splunk’s CTO Snehal Antani, as Splunk announced accelerated momentum in Industrial Data and the IoT. The trend is driven by Splunk’s continued investment in its products and partner ecosystem as well as the creativity of customers and the flexibility to deploy Splunk IoT solutions as software, cloud services or in a hybrid environment. Customers are using Splunk® solutions to collect and correlate data from control systems, sensors, mobile devices and IT systems for a variety of Ind...
As enterprises capture more and more data of all types – structured, semi-structured, and unstructured – data discovery requirements for business intelligence (BI), Big Data, and predictive analytics initiatives grow more complex. A company’s ability to become data-driven and compete on analytics depends on the speed with which it can provision their analytics applications with all relevant information. The task of finding data has traditionally resided with IT, but now organizations increasingly turn towards data source discovery tools to find the right data, in context, for business users, d...
Clearly the way forward is to move to cloud be it bare metal, VMs or containers. One aspect of the current public clouds that is slowing this cloud migration is cloud lock-in. Every cloud vendor is trying to make it very difficult to move out once a customer has chosen their cloud. In his session at 17th Cloud Expo, Naveen Nimmu, CEO of Clouber, Inc., will advocate that making the inter-cloud migration as simple as changing airlines would help the entire industry to quickly adopt the cloud without worrying about any lock-in fears. In fact by having standard APIs for IaaS would help PaaS expl...
Organizations already struggle with the simple collection of data resulting from the proliferation of IoT, lacking the right infrastructure to manage it. They can't only rely on the cloud to collect and utilize this data because many applications still require dedicated infrastructure for security, redundancy, performance, etc. In his session at 17th Cloud Expo, Emil Sayegh, CEO of Codero Hosting, will discuss how in order to resolve the inherent issues, companies need to combine dedicated and cloud solutions through hybrid hosting – a sustainable solution for the data required to manage I...
SYS-CON Events announced today that IBM Cloud Data Services has been named “Bronze Sponsor” of SYS-CON's 17th Cloud Expo, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. IBM Cloud Data Services offers a portfolio of integrated, best-of-breed cloud data services for developers focused on mobile computing and analytics use cases.
SYS-CON Events announced today that ProfitBricks, the provider of painless cloud infrastructure, will exhibit at SYS-CON's 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. ProfitBricks is the IaaS provider that offers a painless cloud experience for all IT users, with no learning curve. ProfitBricks boasts flexible cloud servers and networking, an integrated Data Center Designer tool for visual control over the cloud and the best price/performance value available. ProfitBricks was named one of the coolest Clo...
Learn how IoT, cloud, social networks and last but not least, humans, can be integrated into a seamless integration of cooperative organisms both cybernetic and biological. This has been enabled by recent advances in IoT device capabilities, messaging frameworks, presence and collaboration services, where devices can share information and make independent and human assisted decisions based upon social status from other entities. In his session at @ThingsExpo, Michael Heydt, founder of Seamless Thingies, will discuss and demonstrate how devices and humans can be integrated from a simple clust...
Apps and devices shouldn't stop working when there's limited or no network connectivity. Learn how to bring data stored in a cloud database to the edge of the network (and back again) whenever an Internet connection is available. In his session at 17th Cloud Expo, Bradley Holt, Developer Advocate at IBM Cloud Data Services, will demonstrate techniques for replicating cloud databases with devices in order to build offline-first mobile or Internet of Things (IoT) apps that can provide a better, faster user experience, both offline and online. The focus of this talk will be on IBM Cloudant, Apa...
As more and more data is generated from a variety of connected devices, the need to get insights from this data and predict future behavior and trends is increasingly essential for businesses. Real-time stream processing is needed in a variety of different industries such as Manufacturing, Oil and Gas, Automobile, Finance, Online Retail, Smart Grids, and Healthcare. Azure Stream Analytics is a fully managed distributed stream computation service that provides low latency, scalable processing of streaming data in the cloud with an enterprise grade SLA. It features built-in integration with Azur...
You have your devices and your data, but what about the rest of your Internet of Things story? Two popular classes of technologies that nicely handle the Big Data analytics for Internet of Things are Apache Hadoop and NoSQL. Hadoop is designed for parallelizing analytical work across many servers and is ideal for the massive data volumes you create with IoT devices. NoSQL databases such as Apache HBase are ideal for storing and retrieving IoT data as “time series data.”
SYS-CON Events announced today that HPM Networks will exhibit at the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. For 20 years, HPM Networks has been integrating technology solutions that solve complex business challenges. HPM Networks has designed solutions for both SMB and enterprise customers throughout the San Francisco Bay Area.
Mobile messaging has been a popular communication channel for more than 20 years. Finnish engineer Matti Makkonen invented the idea for SMS (Short Message Service) in 1984, making his vision a reality on December 3, 1992 by sending the first message ("Happy Christmas") from a PC to a cell phone. Since then, the technology has evolved immensely, from both a technology standpoint, and in our everyday uses for it. Originally used for person-to-person (P2P) communication, i.e., Sally sends a text message to Betty – mobile messaging now offers tremendous value to businesses for customer and empl...
The broad selection of hardware, the rapid evolution of operating systems and the time-to-market for mobile apps has been so rapid that new challenges for developers and engineers arise every day. Security, testing, hosting, and other metrics have to be considered through the process. In his session at Big Data Expo, Walter Maguire, Chief Field Technologist, HP Big Data Group, at Hewlett-Packard, will discuss the challenges faced by developers and a composite Big Data applications builder, focusing on how to help solve the problems that developers are continuously battling.
SYS-CON Events announced today that MobiDev, a software development company, will exhibit at the 17th International Cloud Expo®, which will take place November 3-5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. MobiDev is a software development company with representative offices in Atlanta (US), Sheffield (UK) and Würzburg (Germany); and development centers in Ukraine. Since 2009 it has grown from a small group of passionate engineers and business managers to a full-scale mobile software company with over 150 developers, designers, quality assurance engineers, project manage...
SYS-CON Events announced today that Cloud Raxak has been named “Media & Session Sponsor” of SYS-CON's 17th Cloud Expo, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. Raxak Protect automates security compliance across private and public clouds. Using the SaaS tool or managed service, developers can deploy cloud apps quickly, cost-effectively, and without error.
Who are you? How do you introduce yourself? Do you use a name, or do you greet a friend by the last four digits of his social security number? Assuming you don’t, why are we content to associate our identity with 10 random digits assigned by our phone company? Identity is an issue that affects everyone, but as individuals we don’t spend a lot of time thinking about it. In his session at @ThingsExpo, Ben Klang, Founder & President of Mojo Lingo, will discuss the impact of technology on identity. Should we federate, or not? How should identity be secured? Who owns the identity? How is identity ...
SYS-CON Events announced today that Solgeniakhela will exhibit at SYS-CON's 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. Solgeniakhela is the global market leader in Cloud Collaboration and Cloud Infrastructure software solutions. Designed to “Bridge the Gap” between Personal and Professional Social, Mobile and Cloud user experiences, our solutions help large and medium-sized organizations dramatically improve productivity, reduce collaboration costs, and increase the overall enterprise value by bringing ...
Sensors and effectors of IoT are solving problems in new ways, but small businesses have been slow to join the quantified world. They’ll need information from IoT using applications as varied as the businesses themselves. In his session at @ThingsExpo, Roger Meike, Distinguished Engineer, Director of Technology Innovation at Intuit, will show how IoT manufacturers can use open standards, public APIs and custom apps to enable the Quantified Small Business. He will use a Raspberry Pi to connect sensors to web services, and cloud integration to connect accounting and data, providing a Bluetooth...
SYS-CON Events announced today that Micron Technology, Inc., a global leader in advanced semiconductor systems, will exhibit at the 17th International Cloud Expo®, which will take place on November 3–5, 2015, at the Santa Clara Convention Center in Santa Clara, CA. Micron’s broad portfolio of high-performance memory technologies – including DRAM, NAND and NOR Flash – is the basis for solid state drives, modules, multichip packages and other system solutions. Backed by more than 35 years of technology leadership, Micron's memory solutions enable the world's most innovative computing, consumer,...
Nowadays, a large number of sensors and devices are connected to the network. Leading-edge IoT technologies integrate various types of sensor data to create a new value for several business decision scenarios. The transparent cloud is a model of a new IoT emergence service platform. Many service providers store and access various types of sensor data in order to create and find out new business values by integrating such data.