Running a Java Program using Eclipse

Here are some basic steps on how to install Eclipse ( A Java IDE ) and run a Java program using it. IDE means Integrated Development Environment. You may run and create Java programs using plain text and command line, but using an IDE gives you more tools and system integration.

Installation of Eclipse IDE

1. Download the Eclipse IDE from the internet ( just search “Eclipse IDE download” ).

2. Choose the IDE for Java SE ( for plain or Java Client Applications ), or Java EE ( for Java Web Applications, that can be hosted on a Web server ). C and C++ is also available, but we will be using Java this time.

3. Download the Eclipse version suited to your OS version ( Windows 32-bit, Windows-64 bit, Linux and etc… )

4. Eclipse needs JRE ( Java Runtime Environment only ) or JDK ( Also a Java Runtime Environment, with tools and add-ons for Java Development ) . For complete Java software development, JDK is recommended. If you just need some software to run on Java as its requirement, JRE is sufficient enough.

5. Extract the Eclipse file after download. Eclipse runs directly after extraction of the zip files, since it does not need any installation. Just click on the “eclipse.exe” icon.

6. Download JRE or JDK suited to your OS version. After the download, install the JDK / JRE in the eclipse folder.

Running a simple Java program

1. Open a “Workspace” on the Eclipse application

2. Create a new “Java Project” inside the workspace. Just choose the simple Java project available in the selection.

3. Create a “Class” under the Java project. The “Class” always starts with a capital letter. The Class should also have the method “public static void main(String[] args)”

4. Write a “Hello World” print screen inside the “public static void main(String[] args)” method. Use “System.out.println(“Hello World”)” line.

5. Check for compile errors. Java code does not run if it has compile errors.

6. Right click on the “Class” and select “Run as Java Appliacation”

7. See the Console tab in Eclipse and the text “Hello World” should appear.

This will appear on your screen if eclipse has run successfully

 

The Eclipse IDE with a Java Class

 

 

 

 

Advertisements

Java Basics – local and global variables

Java does not have a global variable, but variables for the whole class known as fields. For all other values that are within a block, or within a method it is a local variable.

Question is when to use?

Class fields : can be used if this variable is always accessed by different methods and other classes. Just be careful its value may not be thread safe. Constant variables should also be declares as class fields.

Example:

public class test(){

 //this is a class field.

 int a = 1;

}

Local variable: variables that are used within method scope only. The variables is created once the method is called, then it is destroyed when the method exits.

Example:

public class test(){

 public void doSomething(){

  //this is a local variable

  int a = 1;

 }

}

Difference between EAR and WAR files

Difference between EAR and WAR files

*.war file – this file is a composition of servlet class files. This package is HTTP request/response capable, can be accessed by a URL and can return a page. The contents are Java Classes, JSP files, GIF ( for website images ), and HTML files. Can be deployed on a Tomcat or Jetty servlet container.

*.ear file – this file is the composition of a enterprise application, that is composed of several war and ear files that functions as a single application. ear files are usually complete with HTTP request/response capability, Database access, Exposed API’s and other fucntionalities. EAR files are deployed on application servers such as Weblogic, JBoss or IBM Websphere. If you deploy a war file on application server, it is wrapped to an ear file, for standard deployment.

*.jar file – is the basic java archive file. A compilation of Java Classes that functions as a single application. May include Java Classes only.

Here are some more detailed answers on stackoverflow.com
http://stackoverflow.com/questions/1594667/war-vs-ear-file

Best Programming languages to learn

1. JAVA

Java language came to its height of popularity in the early 2000
There are various reason why Java is still the top programming language until 2014

* Available in all aspects of computing
– Yes, Java is everywhere. Java can be used in making small/medium/large scale websites, Desktop applications and mobile/smartphone application development ( Android is derived from Java). It also has front-end and back-end capabilities.

* Java code is somehow an enhanced and improved version of C++. Java is a compile once run anywhere code. So the same Java code is written for any OS available. Java is also integrated to Web frameworks, Database, Network and almost any computing API, Java is available.

* Java as tends to be a not lightweight and has some performance issues, but not that of major concern. For embedded technologies and faster performance, C and C++ is recommended.

2. C++

* A lot of C++ legacy software are still being used today. This is one of the main reasons why C++ is still being created and added on top of some huge legacy infrastructure. But in most industry trends, I have not encountered fresh projects using C++. So I guess its popularity is from its large legacy applications it has built upon, which simply could not be replaced that easily.

* C is mostly used on Backend and Desktop applications.

3. C  

C is very solid on the microcontroller and firmware programming. It provides the best tool for low level access and lightweight in memory usage. With the rise of Industrial Internet, C language will gain more popularity and will be in demand in the future.

4. Web Technologies

Phyton, is currently the most popular backend script. We also have PHP and JavaScript in the list for the most popular Web programs. Ruby is trailing as an alternative to other web programs, such as Java and PHP. Other popular languages, some old and some new, also appear on the list, such as SQL, HTML, PERL, Visual Basic, Go , and etc…

See the full list in this website:
http://www.sitepoint.com/best-programming-language-learn-2014-mid-year-update/?utm_content=buffer5497d&utm_medium=social&utm_source=facebook.com&utm_campaign=buffer

Bitwise Operations in Java

To create a byte variable in Java try this example:

byte b = (byte) 16;
b = b >> 2;
 

The result of “b” would be 4.

Explanation:

Note that it will be stored in a 32 bit format. so 16 would be:

00000000 00000000 00000000 00010000

Then we shift 2 bits to the left, so the answer would be:

00000000 00000000 00000000 00000100

Thus, we got the decimal value of 4.

Here are the bitwise operations supported by Java

~       Unary bitwise complement ( inverse )
<<      Signed left shift
>>      Signed right shift
>>>     Unsigned right shift
&       Bitwise AND
^       Bitwise exclusive OR ( 1 OR 1 is counted as 0, the rest of the combinations is the same as inclusive OR )
|       Bitwise inclusive OR

 

 

Java Substring

If we have an entire String, for example:

“cebu cm solutions”

we can take part of that String by calling the String.substring(<index>); or by String.substring(<firstindex>, <lastindex>).

if we would like to display “cm solutions”, just use String.substring(5);

if we would like to display “cm” just use String.substring(5,7);

NOTE: Always remember in Java that String is an Object, not a primitive variable

See another tutorial here:

http://www.tutorialspoint.com/java/java_string_substring.htm

Using Property files in Java

Creating configuration files in Java is simple and straightforward. We just have to use the Properties object. The example of a property file is here below:

filename: config.properties ( or you can just create any file name you want, but better follow convention )
content: example.property=skeep

Here is a sample code to get the “skeep” String , which is defined in “example.property”

– Define Properties object and InputStream object. Properties can process a “key=property” content, while InputStream is for file reading.
– Define the property file location. In this example, we just use the relative path of the project. Therefore file name and path should be included.
– Use FileInputSteam() constructor to read the property file, then use the getProperty() method to retrieve the property content of a key.
– It is proper to use a close input stream in a finally block, in case file access will fail.
– Properties class can also read integers.
– We can add more “key-property” values in the properties file.

Properties prop = new Properties();
InputStream input = null;
 try{
  input = new FileInputStream("config.properties");
  prop.load(input);
  sensitive_words = prop.getProperty("example.property");
 } finally {
   if (input != null) {
    try {
      input.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
   }
 }