Categories
Java Uncategorized

Java 101 – Lesson 2 – Getting familiar with Java

I’ve never been too fond of books when trying to learn a language for the first time. The reason is that they give you too many details in the beginning that you may not necessarily be interested in until later on. The approach that I’ve found to be very useful in learning or teaching is to let the user/student get an overview first and then go into how things actually work. So, in this article we are going to write a simple application/program and run it. As we move on, I will explain what is going on.

Hello World

For those who don’t know, Hello world is a very popular program in the computing world. Almost every language’s tutorial has one for beginners. All it does is print Hello world on the screen in one form or another. This is what we will do too. We will write a program that will print “Hello world” in your console window.

Create a folder in your c:\ drive and name it java_projects. This is where we will place all Java files.

Open notepad and type this in it :

import java.io.*;

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

After copying and pasting the above code in your notepad window, save the file as HelloWorld.java in c:\java_projects\ . When you save it, make sure that file type is selected to “All files” or notepad will save your file as “HelloWorld.java.txt” and not “HelloWorld.java” .

Saving Java file
Saving Java file

Oh, and you will have to save the file as HelloWorld.java (with the capital letters) or the code won’t compile. I will get to the reason in later articles.

We have written the code. Now, we need to compile and run it. We will do that by opening the command prompt (also referred to as the console) and do it from there. Click on start then on Run and type cmd in the dialog box that opens up.

Run dialog
Run dialog

A black window will appear with a blinking cursor. It is the command prompt. Type this in it :

cd c:\java_projects\

Then type :

javac HelloWorld.java

The screen will get stuck for a few seconds and then you will see a blinking cursor again. javac is used to compile java code into byte-code. If you go to c:\java_projects\ in windows explorer, you will see that there javac created a file called HelloWorld.class . This is the byte-code file that the command “java” needs to run the application. You can run your code by typing :

java HelloWorld

A line of text saying Hello world should appear in your console. Congratulations ! you just ran your first application.

Click to enlarge
Click to enlarge
Categories
Java Uncategorized

Java 101 – Lesson 1 – Installing the JDK

Since this is the first class, I guess I should talk about what Java is. Java is a language that can be used to write platform-independent desktop applications or web applications. The term “Platform independent” means that you can write the code for an application on Windows and run it in Linux or a Mac (as long as you have a JVM installed). In fact, you can run your program on any device – not just computers – if that device has a JVM available for it.

The story of how Java came into existence is very interesting. The first release of Java was in 1995. In the last 14 years, Java has come a long way from being something that just integrated into Netscape to being one of the most widely used languages in the computer industry.¬† I don’t want to bore everyone with the details of the history of Java’s birth so those who are interested can go to : http://java.sun.com/features/1998/05/birthday.html

How does Java work, you ask? When you write code in Java, you need to compile it using the javac command. When you compile your code, it is converted to “byte-code”. This byte-code is later read by JVM when you run your application using the java command. So, it’s a 2 step process.

To be able to make program in Java, you need to install the JDK (Java Development Kit). JDK comes with quite a few things but the ones we are interested in are the compiler, and the JVM. You may also have heard the term JRE (if you’ve ever had to install the JRE plugin for Internet Explorer). JRE stands for Java Runtime Environment. You can run Java applications with it but you can’t compile them.

Let’s get started with the installation then. Download the latest version of JDK from : http://java.sun.com/javase/downloads/index.jsp . The latest version of JDK is JDK 6 update 12 as of this writing. Here’s a link to download JDK6 update12 https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/[email protected]_Developer. Select your platform, etc and depending on your the speed of your internet connection, it should be done fairly quickly.

Double click the file you just downloaded and it will start the installation process. Follow the instructions that come up and after it’s done you should be ready to write code Java code – assuming nothing went wrong.

J2SE download
J2SE download

For Windows :

You have java installed, but now you need to add it to your path. Path is an environment variable in windows. All the folders in this variable are searched for, when you try to run a program without providing its location.

Click start then right click on “My Computer”. Select “Properties” from the menu that pops up. Go to the “Advanced” tab and press the “Environment variables” button in it.

Getting to system properties
Click to enlarge
Environment Variables
Click to enlarge

In the dialog that opens up, you will find a variable called path. Click on it and click on the edit button. If you have installed the JDK in the default path, then your JDK will be installed in C:\Program Files\Java. You need to get the location for the bin folder in your JDK. For, JDK6 Update12 it is : C:\Program Files\Java\jdk1.6.0_12\bin . Go to the end in the “Variable value” textbox and type ; then type in the location to the bin folder for your JDK. In the end you will have written something like this :

;C:\Program Files\Java\jdk1.6.0_12\bin

Here’s a few pictures to make things easier ūüôā

Click to enlarge
Click to enlarge
Click to enlarge
Click to enlarge
Click to enlarge
Click to enlarge

And, viola! You’re done.

Categories
Android Uncategorized

Android Exeda – A more detailed look

I had written a post about CompuLab releasing an Android based phone called Exeda. It’s official now and they are making it available in March. They will make the price available along with the phone. It has a 3.5 inch screen which is sun-readable. It also has a touchpad which acts as a mouse. The phone itself seems very small in the picture so it might not be meant for big fingers. The keys are very small in size. It also comes with Windows Mobile and they say they will add support for Linux in the future (yay! Linux).

According to its maker

“Exeda is designed to serve as an enterprise digital assistant (EDA). Comprised of embedded components, the exeda has guaranteed long-term availability and can be custom configured when ordered in volume. “

For more information go to http://www.exedamobile.com/web/?gclid=CJHqrvG4qJgCFQrAGgodLyYvnw and http://www.exedamobile.com/web/index.php?option=com_content&view=article&id=3&Itemid=7

Exeda
Exeda
Exeda
Exeda
Categories
Uncategorized

Action: Sponsors Sought for U.S. Citizens to be Evacuated from Gaza

(WASHINGTON, D.C., 1/12/2009) – CAIR today issued a call for American sponsors willing to assist families of U.S. citizens who may be evacuated this week from the Gaza Strip.

SEE: U.S. Preparing Evacuation of 150 Americans

State Department officials tell CAIR that some of the almost 150 potential evacuees do not have family members in the United States.A number of those seeking evacuation are non-citizen parents of young American-born children.¬†CAIR is calling on individuals and relief groups to offer support.¬†Potential sponsors would have to provide all the needs of evacuees for a period of six months, demonstrate financial ability and submit an I-134 ‚ÄúAffidavit of Support‚ÄĚ form.

CAIR has been seeking State Department assistance in evacuating two American children, ages six and three, along with all the other Americans in Gaza. The father of the two children said bullets fired from an Israeli tank hit the family home in Beit Hanoun on Saturday. He said his brother’s nearby two-story home was destroyed today by the Israelis.

The death toll from Israeli attacks on Gaza has topped 900, with many women and children among the dead and more than 4000 injured. In its attacks, Israel has denied access to wounded civilians and has targeted homes, schools, mosques, universities, medical and United Nations personnel, a hospital, and a church. A leading human rights group has accused Israel of using white-phosphorus munitions in its attacks on civilian areas.

SEE: 1 Dead, Dozens Injured in Gaza by Suspected White Phosphorus Munitions
SEE ALSO: Gaza War Comparable to Sabra and Shatila Massacre: Medics (AFP)
Israeli Missile Reduces Church-Backed Hospital to Rubble

The United Nations’ top human rights official said Friday that the Israeli military may have committed war crimes in Gaza.

SEE: UN Human Rights Chief Accuses Israel of War Crimes
SEE ALSO:¬†Gaza “No Place for Civilians”: ICRC¬†(Reuters)
UN Human Rights Council Condemns Israeli Military Action

ACTION REQUESTED:

Potential sponsors must be willing to provide all of the needs of the evacuees for a period of six months. Sponsors would also have to demonstrate the financial ability to sponsor and submit an I-134 ‚ÄúAffidavit of Support‚ÄĚ form.

Those who are willing to meet these strict requirements should send an e-mail to:[email protected]

Categories
Uncategorized

Israeli Bullets Hit Home of American Children Trapped in Gaza

(WASHINGTON, D.C., 1/12/2009) – The Council on American-Islamic Relations (CAIR) reported today that Israeli bullets hit the house of two American children trapped under bombardment by Israel in the Gaza Strip.

CAIR has been seeking State Department assistance in evacuating the two children, ages six and three, along with all the other Americans in Gaza. Friends of the family contacted CAIR seeking help. (Photos of the family are available from CAIR.)

Reached by phone in Gaza, the father of the American children said bullets fired from an Israeli tank hit the family home in Beit Hanoun on Saturday. He said a nearby two-story home owned by his brother was destroyed today by the Israelis. The father, who graduated from George Washington University in Washington, D.C., said he has recently been contacted by American officials about a possible evacuation.

In a response to a letter CAIR sent January 7 to Secretary of State Condoleezza Rice, the State Department said that an evacuation is being planned this week for almost 150 American citizens and their family members trapped in Gaza. Officials say 16 American citizens were evacuated on January 2. American sponsors are also being sought for those evacuees who may be brought to the United States. A number of potential evacuees are non-citizen parents of young American-born children.

“We call for the immediate intervention of the Bush administration to demand that Israel stop its attacks on civilian areas so that all the American citizens facing death in Gaza can be safely evacuated,” said CAIR National Communications Director Ibrahim Hooper. ‚ÄúEach day the evacuation is delayed means another day in which U.S. citizens might be injured or killed by American taxpayer-funded weapons in a brutal campaign supported by our own government.‚ÄĚ

He said citizens of many other nations, including Canada, Russia, the Philippines, Ukraine, Denmark, Austria, Norway, Romania, and Sweden have already been evacuated from Gaza.

Hooper noted that the death toll from Israeli attacks on Gaza has topped 900, including many women and children. In its attacks, Israel has denied access to wounded civilians and has targeted homes, schools, mosques, universities, medical and United Nations personnel, and a church. A leading human rights group yesterday accused Israel of using white-phosphorus munitions in its attacks on civilian areas.

SEE: Israel Accused of Using White Phosphorus in Gaza (Reuters)

The United Nations’ top human rights official said Friday that the Israeli military may have committed war crimes in Gaza.

SEE: UN Human Rights Chief Accuses Israel of War Crimes

CAIR, America’s largest Islamic civil liberties group, has 35 offices and chapters nationwide and in Canada. Its mission is to enhance the understanding of Islam, encourage dialogue, protect civil liberties, empower American Muslims, and build coalitions that promote justice and mutual understanding.

Categories
Android Uncategorized

G2 on the way?

According to some rumors on IT news websites, some new Android phones will be released in 2009. cellphonesignal.com claims that a phone, G2, is in the making and will be released in January of 2009. Another website claims that it will be released in April. Best of all, G2 will not be locked down to T-Mobile. It will be great to have a phone not locked down to T-mobile but this begs the question : WIll this phone be able to access T-mobile’s 3G services? And if yes, will it also be able to access 3G service of providers from the rest of the world who don’t use 1700 Mhz for 3G? I guess we will soon find out.¬†

An Australian company had announced the release of an Android based phone called Kogan Agora. It looks something like Samsung Blackjack and has no touch screen (as far as I can tell). A chinese firm, Lenovo, has also announced an Android based phone which will have touch screen but no physical keyboard. It looks something like HTC Touch HD from the front. The phone is called QIGI.

Looks like 2009 has some great things in store for all the phone lovers out there. Happy New Year everyone !

Categories
Uncategorized

Debugging services in C#

Recently, I have had to build a service for Windows and (not unlike everyone else who has tried to do this) I came across a hurdle of not being able to debug my service from the Visual Studio. I had to install the service manually, start it and debug it. Every time after the installation, I have to stop the service before I compile the code, then restart it again after the file has been copied. This can get very annoying if you have to do this multiple times a day. It’s a debugging nightmare for a developer. ¬†However, you needn’t fear anymore as I have a solution to all of this chaos. You can edit Program.cs of ¬†your project and insert this code in there :

 

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        static void Main()
        {
            #if (!DEBUG)
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);
            #else
            Service1 service = new Service1();
            service.Start();
            System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
            #endif 
        }
    }

 

What the code above will do is, if you are debugging your service, it will start your service as a thread and run it till you stop it. If you are compiling the code for a release, then it will run the whole thing as a regular Windows Service.

Categories
Android Uncategorized

Tmobile G1, AT&T and 3G

Google has released unlocked versions of Tmobile G1 for developers. It costs about $500 and you have to register with Google as a merchant for Android apps ($25 registration fee). Most developers who were (and still are) enthusiastic about G1 and Android failed to realize that G1’s (and Tmobile’s ) 3G network functions on the 1700 band and all other prodivers in USA (including AT&T) operate their 3G at another band (I think it’s 1900 or 2100). So, if you get G1 and your service provider is not Tmobile, then you wont be able to use the 3G service of your provider (unless their 3G service operates on the 1700 band).

Maybe Google should put a huge disclaimer on their site stating this so developers (and other people) can make an informed decision about getting an unlocked G1. ¬†Having said that, I am not trying to blame Google or TMobile. They haven’t tried to hide the fact that their 3G service works on a different band, and it *is* the consumer’s responsibility to make sure they know what they are getting.

The same problem is with AT&T’s iPhone. You can jailbreak it but you lose some of its functionality (like visual voicemail) because AT&T had to make custom applications to support iPhone’s interaction with it’s network. I hope this doesn’t become a trend where a phone is locked to a provider. It would be a huge step backwards.

Categories
Android Uncategorized

Displaying dialogs in Android 1.0

When I say dialogs, I mean the little alert window that pops up and lets you know about an error that occurred or warns you about something you can’t do. It locks up the whole screen and demands you pay attention to it. I use a dialog to display information about the application.

Android has a few type of alert dialogs. One is a dialog that will not go away unless you make it go away. The other is a dialog that goes away after soem time. We will take a look at both of them here. 

First, let’s look at dialogs that don’t go away unless you tell them to. Here is the code for them :

 

 

public class SomeActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstance)  {
        AlertDialog.Builder dlgAlert  = new AlertDialog.Builder(this);
¬†¬† ¬† ¬† ¬†dlgAlert.setMessage(“This is an alert with no consequence”);
¬†¬† ¬† ¬† ¬†dlgAlert.setTitle(“App Title”);
¬†¬† ¬† ¬† ¬†dlgAlert.setPositiveButton(“OK”, null);
        dlgAlert.setCancelable(true);
        dlgAlert.create().show();
    }
} 

The code above will display an alert with a title, a message and an OK button at the bottom. You don’t really have to set the title, the ok button or the message but it helps to do so. If you don’t use an “OK” (or whatever) button, the dialog will never go. The user will have to close the application in order to get out of it. There are other types of buttons that you can use (and you can use multiple buttons also). You can take a look at AlertDialogBuilder’s API for more information here.¬†

The second type of dialog is more of a notification alert that is displayed on the main screen. I have used it before in my previous post which spoke about how to set alarms in Android. This alert will be shown for some time and regardless of whether a user responds to it or not, it will disappear after some time. Below is the code that is used to call it (or display it) :

 

Toast.makeText(con, “This is a message from hell. Yes my friend, HELL”, Toast.LENGTH_SHORT).show();

 

The above code will display an alert for a short amount of time and then close it. This alert is very simple and short and doesn’t require much to be displayed. You can also use Toast.LENGTH_LONG to display the alert for a longer period of time. And, like always below are the screenshots of what both of them look like.

 

The dialog that requires user to click something
The dialog that requires user to click something

 

The disappearing alert
The disappearing alert
Categories
Android Uncategorized

Sending email from Android

Sending an email is a very basic functionality (for some applications anyway). Android makes it very easy to do this. All you have to do is create a new intent, setup the content of the email and send it. 

Intent in = new Intent(Intent.ACTION_SEND); 
in.putExtra(Intent.EXTRA_TEXT, “this is the body of the email”);¬†
in.putExtra(Intent.EXTRA_SUBJECT, “Subject”);¬†
in.setType(“message/rfc822”);¬†
startActivity(Intent.createChooser(in, “Title:”));