In last article we have gone through some basic steps of setting the environment for writing and executing java code and gone through some little less comfortable way of compiling and running a java code.
We also told you that there is a simple way to make our life pleasant. Eclipse to the rescue.
Obviously, this is not an Eclipse tutorial, so just a crash course for setting up eclipse for you.
Eclipse is the most popular and standard tool for developing and running Java code. All you need to do is go to this following URL and download the latest version of eclipse: http://www.eclipse.org/downloads
Can you see: Eclipse IDE for Java EE Developers? Ok then, download according to the windows processor type (32/64 bits). (If you have any other operating systems, then do a simple Google search for your OS, like download Eclipse for Mac, for example)
Now first create some folders as said here. Say you have created a folder tree in your D: drive as LearnJava/Workspace/CoreJavaProject. (don’t you dare try to think what is this folder, just do it for now).
After downloading eclipse, there is no need to install anything, just double click on the eclipse icon. A new window will open and eclipse will ask you to select a workspace. Select the folder you just have created by browsing, that is the CoreJavaProject folder under Workspace (which is under Learnjava). That’s it, the secret door of eclipse is now open for you.
After opening eclipse, go to File -> New -> Other. A new window will open, type 'java' in the text box, then select 'Java Project'.
Click 'Next'. Give any desired name for the project, then click 'Finish'.
Your java project is ready to work with.
Select Windows -> Show View -> Navigator. (This part is optional, but I can guarantee, if you do this, the folder structure will look much more understandable) In case you do not find the option 'Navigator', select the last option 'Other', a new window will come, and then type 'Navigator'.
Now you can see there is a folder 'src' under your project folder. I have given the project name as 'CoreJava'.
Now, right click on 'src', then 'new -> package '. (Again, in case you do not find the 'package' option, go to the last option 'Other', then type package).
In the name field type 'com.java', then click 'Finish'.
If you have done everything correctly then your window will look like this:
Surprisingly, you are done with the required project set up in eclipse.
All is remaining to do is to create and run java file. A walk in the park.
Right Click on 'java' folder, and then new -> Class (again, you know what to do if you don’t find the option 'Class')
In the Name field type 'LearnJava', then click 'Finish'.
A new Class has been created only for you, now in the Class, type the code like the last time, make it look like below:
public class LearnJava
public static void main(String args)
System.out.println("In life, sooner or later, you have to learn Java");
Again, just do not think about the first line - package com.java; for now. I make a promise that I will tell you what that is in details. For now just know that since you have created a package ‘com.java’, the class which you are writing will have the statement: package com.java; at the very first line. And since you are using eclipse you don’t have to write that line on your own, eclipse will do that for you.
Save the file (Ctrl + S)
Now right click on the file, Run As -> Java Application. Can you see the output in the console? Congrats, you have written your first code in eclipse.
From now on, we will do our coding in eclipse only. So next time you come to our blog, open your eclipse at the same time. Later, when we will cover some more path towards becoming 'expert', we will discuss what is workspace, or why out of so many options we choose 'Java Project' only , whether choosing other option would have solved our purpose or not, this kind of finer details.
Our main topic for today is Syntax. Do you remember who checks the syntax for your program? Yes, java Compiler. Java compiler has a sole job of checking syntax of your code and letting you know in which line what kind of syntactical error you have made (if any of course). Since we have learned to use eclipse, we will use the features of eclipse fully.
(In case you are wondering, eclipse has a built in java compiler with it, that compiler compiles your code)
First modify the code you have written just a little, make it look like this:
/* My journey becomes here towards becoming a Java Expert. */
public class LearnJava
public static void main(String args) // main method
System.out.println("In life, sooner or later, you have to learn Java"); // output to console
The program still remains the same, that is it will still produce the same output as before.