Welcome!

Java IoT Authors: Elizabeth White, Yeshim Deniz, Tim Hinds, Douglas Lyon, Stackify Blog

Related Topics: Eclipse

Eclipse: Article

SYS-CON Webcast: Eclipse IDE for Students, Useful Eclipse Tips & Tricks

Lesson 10 In The Hugely Popular "Java Basics" SYS-CON.TV Education Series by Yakov Fain

In Lesson 10 of the Java Basics series Yakov Fain shows you how to start working with Eclipse IDE, which is a tool of choice for millions of professional Java programmers. After reading this article you may want to look at another of Yakov's articles for youngsters "Teaching Kids Programming: Even Younger Kids can Learn Java".

Moving to Eclipse

Programmers usually work in a so-called Integrated Development Environment (IDE). You can write, compile and run programs there. An IDE also has a Help thingy that describes all elements of the language, and makes it easier to find and fix errors in your programs. While some IDE programs are expensive, there is an excellent free IDE called Eclipse. You can download it from the Web site www.eclipse.org. In this chapter I'll help you to download and install Eclipse IDE on your computer, create a project there called Hello World, and after this we'll be creating all our programs there.

Make yourself comfortable in Eclipse - it's an excellent tool that many professional Java programmers use.

Installing Eclipse

Open the Web page www.eclipse.org and click on the Download menu on the left (http). Click on the link Main Eclipse Download Site and select the version of Eclipse you want to download. They usually have one latest release and several stable builds. The latest release is an officially released product. Even though stable builds may have more features, they still may have some minor problems. At the time of this writing the latest stable build is 3.0M8.

Select this build and you'll see the following window:

 

Click on the link (http) next to the word Windows, Mac, or Linux depending on your computer, and download the file with this long name that ends with .zip to any folder on your disk.

 

 

Installation of Eclipse is complete! For your convenience, create the shortcut for Eclipse on your desktop. Right-click on the Windows desktop, then press New, Shortcut, Browse, and select the file eclipse.exe in the folder c:\eclipse. To start the program, double-click on the blue icon Eclipse, and you'll see the first Welcome screen (this screen is changing slightly with each Eclipse build):

 

If your screen looks different, proceed to so-called Workbench, which is the working area for your Java projects.

Getting Started with Eclipse

In this section I'll show you how you can quickly create and run Java programs in Eclipse. You can also find a nice tutorial under the menus Help, Help Contents, and Java Development User Guide.

To start working on a program you'll need to create a new project. A simple project like our HelloWorld will have just one file - HelloWorld.java. Pretty soon we'll create more advanced projects that will consist of several files.

To create a brand new project in Eclipse just click on the menus File, New, Project, and then press the button Next on the New Project window. Now you'll need to enter the name of your new project, for example My First Project:

 

Look at the grayed out box Directory. It tells you where the files of this project will be located on the disk. Eclipse has a special folder workspace, where it keeps all files for your projects. Later on, you can create separate projects for a calculator program, a Tic-Tac-Toe game, and other programs.

Eclipse workbench has several smaller areas called perspectives which are different views of your projects.

 

If you click on the little plus sign by My First Project, it'll expand showing you an item Java Run-time Environment (JRE) System Library which is a part of the project. If for any reason you do not see JRE there, click on the menus Windows, Preferences, Java, Editor, Installed JREs, Add, and, using the button Browse find the folder where you have installed Java, for example c:\j2sdk1.5.0.

Creating Programs in Eclipse

Let's recreate the HelloWorld program from Chapter 1 of my e-book Java Programming for Kids, Parents and Grandparents in Eclipse. Java programs are classes that represent objects from real life. You'll learn more about classes in the next chapter.

To create a class in Eclipse select the menus File, New, Class and enter HelloWorld in the field Name. Also, in the section Which methods stubs you would like to create, check off the box

public static void main(String[] args)

 

Press the button Finish, and you'll see that Eclipse created for you the class HelloWorld. It placed program comments (the text between /* and */) on top - you should change them to describe your class. After the comments you'll find the code of the class HelloWorld with an empty method main(). The word method means action. To run a Java class as a program, this class must have a method called main().

 

To complete our program, place the cursor after the curly brace in the line with main, push the button Enter and type the following on the new line:

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

To save the program on disk and compile it, just press at the same time two buttons on your keyboard: Ctrl-S. If you did not make any syntax errors, you won't see any messages - the program is compiled. But let's make an error on purpose to see what's going to happen. Erase the last curly brace and hit Ctrl-S again. Eclipse will display the Unmatched Brace error in the tasks perspective, and also it will place a red mark by the line that has a problem.

As your projects become larger, they'll have several files and compiler may generate more than one error. You can quickly find (not fix though) the problematic lines by double-clicking on the error message in the tasks perspective. Let's put the curly brace back and hit Ctrl-S again - voila, the error message is gone!

Running HelloWorld in Eclipse

Our simple program is a one-class project. But pretty soon your projects will have several Java classes. That's why before running the project for the first time, you need to tell Eclipse which class in this project is the main one.

Select the menu Run, then Run...(make sure that Java Application is selected in the top left corner), and enter the names of the project and the main class:

 

Now press the button Run, to start the program. It will print the words Hello World in the console view the same way as it did in Chapter 1.

You can run this project by selecting the menus Run, Run Last Launched or by pressing the buttons Ctrl-F11 on the keyboard.

How HelloWorld Works?

Let's start learning what's actually happening in the program HelloWorld.

The class HelloWorld has only one method main(), which is an entry point of a Java application (program). You can tell that main is a method, because it has parentheses after the word main. Methods can call (use) other methods, for example our method main() calls the method println() to display the text Hello World on the screen.

Each method starts with a declaration line called a method signature:

public static void main(String[] args)

This method signature tells us the following:

  • Who can access the method - public. The keyword public means that the method main() could be accessed by any other Java class or JVM itself.
  • Instructions on how to use it - static. The keyword static means that you don't have to create an instance (a copy ) of HelloWorld object in memory to use this method. We'll talk about instances more in the next chapter.
  • Does the method return any data? The keyword void means that the method main() doesn't return any data to the calling program, which is Eclipse in this case. But if for example, a method had to perform some calculations, it could have returned a resulting number to its caller.
  • The name of the method is main.
  • The list of arguments - some data that could be given to the method - String[] args. In the method main() the String[] args means that this method can receive an array of Strings that represent text data. The values that are being passed to a method are called arguments.

As I said before, you can have a program that consists of several classes, but one of them has the method main(). Java class usually have several methods. For example, a class Game can have the methods startGame(), stopGame(), readScore(), and so on.

The body of our method main()has only one line :

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

Every command or a method call must end with a semicolon ;. The method println()knows how to print data on the system console (command window). Method names in Java are always followed by parentheses. If you see a method with empty parentheses, this means that this method does not have any arguments.

The System.out means that the variable out is defined inside the class System that comes with Java. How are you supposed to know that there's something called out in the class System? Eclipse will help you with this. After you type the word System and a dot, Eclipse will show you everything that is available in this class. At any time you can also put a cursor after the dot and press Ctrl-Space to bring up a help box similar to this one:

 

The out.println() tells us that there is an object represented by a variable out and this "something called out" has a method called println(). The dot between a class and a method name means that this method exists inside this class. Say you have a class PingPongGame that has a method saveScore(). This is how you can call this method for Dave who won three games:

PingPongGame.saveScore("Dave", 3);

Again, the data between parentheses are called arguments or parameters. These parameters are given to a method for some kind of processing, for example saving data on the disk. The method saveScore() has two arguments -a text string "Dave", and the number 3.

Eclipse will add fun to writing Java programs. The Appendix below has some useful tips and tricks that will speed up your Java programming in this excellent IDE.

Appendix: Eclipse Tips

Eclipse has many little convenient commands that make Java programming a little easier. I've included some useful Eclipse tips here, but I'm sure you'll find more when you start using this tool.

  • If you see a little asterisk in the tab with the class, this means that the class has unsaved code changes.
  • Highlight the name of the class or a method that is used in your code and press the button F3 on your keyboard. This will take you to the line where this class or method was declared.
  • If some of the lines are marked with red error circles, move the mouse over the circle to see the error text.
  • Press Ctrl-F11 to run the last-launched program again.
  • Place the cursor after a curly brace and Eclipse will mark the matching brace.
  • To change the superclass when creating a new class, click on the button Browse, delete the class java.lang.Object and enter the first letter of the class you'd like to use. You'll see a list of available classes to choose from.
  • To copy a class from one package to another, select the class and press Ctrl-C. Select the destination package and press Ctrl-V.
  • To rename a class, a variable or a method, right-click on it and select Refactor and Rename from the popup menu. This will rename every occurrence of this name.
  • If your project needs some external jars, right-click on the project name, select Properties, Java Build Path and press the button Add External Jars.

Eclipse Debugger

Rumor has it that about 40 years ago, when computers were large and would not even fit in your room, all of a sudden one of the programs started giving wrong results. All these troubles were caused by a small bug that was sitting inside the computer somewhere in the wires. When people removed the bug, the program started working properly again. Since then, to debug a program means to find out why it does not give the expected results.

Do not confuse bugs with the compilation errors. Say for example, instead of multiplying the variable by 2, you'll multiply it by 22. This typo will not generate any compilation errors, but the result will be incorrect. Debuggers allow you to step through a running program one line at a time, and you can see and change values of all variables at each point of the program execution.

 

I'll show you how to use Eclipse debugger using the FishMaster program from Chapter 4 of my e-book  Java Programming for Kids, Parents and Grandparents.

A breakpoint is a line in the code where you'd like program to pause so you can see/change current values of the variables, and some other run-time information. To set a breakpoint just double click on the gray area to the left of the line where you want a program to stop. Let's do it in the FishMaster class on the line myFish.dive(2). You'll see a round bullet on this line which is a breakpoint. Now, select the menus Run, Debug.... Select the application FishMaster and press the button Debug.

FishMaster will start running in the debug mode, and as soon as the program reaches the line myFish.dive(2), it will stop and will wait for your further instructions.

You will see a window similar to the next one.

 

In the left bottom part of the debug perspective, you see that the line with the breakpoint is highlighted. The blue arrow points at the line that is about to be executed. On the right side (in the Variables view) click on the little plus sign by the variable myFish. Since this variable points at the object Fish, you will see all member variables of this class and their current values, for example currentDepth=20.

The arrows in the top left area allow you to continue execution of the program in different modes. The first bended yellow arrow means step into the method. If you press this arrow (or F5), you'll find yourself inside the method dive(). The window changes and you see the values of the argument howDeep=2 as in the next screenshot. Click on the little plus by the word this to see what are the current values of member variables of this object.

To change the value of the variable, right-click on the variable and enter the new value. This can help when you are not sure why the program does not work correctly and would like to play what if game.

 

To continue execution one line at a time, keep pressing the next arrow step over (or the button F6).

If you want to continue program in the fast mode, press a small green triangle or the button F8.

To remove the breakpoint just double-click on the little round bullet and it'll disappear. I like using debugger even if my program does not have a bug - it helps me better understand what exactly happens inside the running program.

Where to put a breakpoint? If you have an idea which method gives you problems, put it right before the suspicious line of code. If you are not sure, just put it in the first line of the method main() and slowly walk through the program.

 

More Stories By Yakov Fain

Yakov Fain is a Java Champion and a co-founder of the IT consultancy Farata Systems and the product company SuranceBay. He wrote a thousand blogs (http://yakovfain.com) and several books about software development. Yakov authored and co-authored such books as "Angular 2 Development with TypeScript", "Java 24-Hour Trainer", and "Enterprise Web Development". His Twitter tag is @yfain

Comments (21)

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.


@ThingsExpo Stories
BnkToTheFuture.com is the largest online investment platform for investing in FinTech, Bitcoin and Blockchain companies. We believe the future of finance looks very different from the past and we aim to invest and provide trading opportunities for qualifying investors that want to build a portfolio in the sector in compliance with international financial regulations.
A strange thing is happening along the way to the Internet of Things, namely far too many devices to work with and manage. It has become clear that we'll need much higher efficiency user experiences that can allow us to more easily and scalably work with the thousands of devices that will soon be in each of our lives. Enter the conversational interface revolution, combining bots we can literally talk with, gesture to, and even direct with our thoughts, with embedded artificial intelligence, whic...
Imagine if you will, a retail floor so densely packed with sensors that they can pick up the movements of insects scurrying across a store aisle. Or a component of a piece of factory equipment so well-instrumented that its digital twin provides resolution down to the micrometer.
In his keynote at 18th Cloud Expo, Andrew Keys, Co-Founder of ConsenSys Enterprise, provided an overview of the evolution of the Internet and the Database and the future of their combination – the Blockchain. Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settle...
Product connectivity goes hand and hand these days with increased use of personal data. New IoT devices are becoming more personalized than ever before. In his session at 22nd Cloud Expo | DXWorld Expo, Nicolas Fierro, CEO of MIMIR Blockchain Solutions, will discuss how in order to protect your data and privacy, IoT applications need to embrace Blockchain technology for a new level of product security never before seen - or needed.
Leading companies, from the Global Fortune 500 to the smallest companies, are adopting hybrid cloud as the path to business advantage. Hybrid cloud depends on cloud services and on-premises infrastructure working in unison. Successful implementations require new levels of data mobility, enabled by an automated and seamless flow across on-premises and cloud resources. In his general session at 21st Cloud Expo, Greg Tevis, an IBM Storage Software Technical Strategist and Customer Solution Architec...
Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, discussed some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he covered some of the best practices for structured team migration an...
No hype cycles or predictions of a gazillion things here. IoT is here. You get it. You know your business and have great ideas for a business transformation strategy. What comes next? Time to make it happen. In his session at @ThingsExpo, Jay Mason, an Associate Partner of Analytics, IoT & Cybersecurity at M&S Consulting, presented a step-by-step plan to develop your technology implementation strategy. He also discussed the evaluation of communication standards and IoT messaging protocols, data...
Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, discussed how from store operations and ...
In his session at 21st Cloud Expo, Raju Shreewastava, founder of Big Data Trunk, provided a fun and simple way to introduce Machine Leaning to anyone and everyone. He solved a machine learning problem and demonstrated an easy way to be able to do machine learning without even coding. Raju Shreewastava is the founder of Big Data Trunk (www.BigDataTrunk.com), a Big Data Training and consulting firm with offices in the United States. He previously led the data warehouse/business intelligence and B...
"IBM is really all in on blockchain. We take a look at sort of the history of blockchain ledger technologies. It started out with bitcoin, Ethereum, and IBM evaluated these particular blockchain technologies and found they were anonymous and permissionless and that many companies were looking for permissioned blockchain," stated René Bostic, Technical VP of the IBM Cloud Unit in North America, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventi...
When shopping for a new data processing platform for IoT solutions, many development teams want to be able to test-drive options before making a choice. Yet when evaluating an IoT solution, it’s simply not feasible to do so at scale with physical devices. Building a sensor simulator is the next best choice; however, generating a realistic simulation at very high TPS with ease of configurability is a formidable challenge. When dealing with multiple application or transport protocols, you would be...
Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
We are given a desktop platform with Java 8 or Java 9 installed and seek to find a way to deploy high-performance Java applications that use Java 3D and/or Jogl without having to run an installer. We are subject to the constraint that the applications be signed and deployed so that they can be run in a trusted environment (i.e., outside of the sandbox). Further, we seek to do this in a way that does not depend on bundling a JRE with our applications, as this makes downloads and installations rat...
Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
DX World EXPO, LLC, a Lighthouse Point, Florida-based startup trade show producer and the creator of "DXWorldEXPO® - Digital Transformation Conference & Expo" has announced its executive management team. The team is headed by Levent Selamoglu, who has been named CEO. "Now is the time for a truly global DX event, to bring together the leading minds from the technology world in a conversation about Digital Transformation," he said in making the announcement.
In this strange new world where more and more power is drawn from business technology, companies are effectively straddling two paths on the road to innovation and transformation into digital enterprises. The first path is the heritage trail – with “legacy” technology forming the background. Here, extant technologies are transformed by core IT teams to provide more API-driven approaches. Legacy systems can restrict companies that are transitioning into digital enterprises. To truly become a lead...
Digital Transformation (DX) is not a "one-size-fits all" strategy. Each organization needs to develop its own unique, long-term DX plan. It must do so by realizing that we now live in a data-driven age, and that technologies such as Cloud Computing, Big Data, the IoT, Cognitive Computing, and Blockchain are only tools. In her general session at 21st Cloud Expo, Rebecca Wanta explained how the strategy must focus on DX and include a commitment from top management to create great IT jobs, monitor ...
"Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offer guided learning experiences on AWS, Azure, Google Cloud and all the surrounding methodologies and technologies that you need to know and your teams need to know in order to leverage the full benefits of the cloud," explained Alex Brower, VP of Marketing at Cloud Academy, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clar...
The IoT Will Grow: In what might be the most obvious prediction of the decade, the IoT will continue to expand next year, with more and more devices coming online every single day. What isn’t so obvious about this prediction: where that growth will occur. The retail, healthcare, and industrial/supply chain industries will likely see the greatest growth. Forrester Research has predicted the IoT will become “the backbone” of customer value as it continues to grow. It is no surprise that retail is ...