Saturday, June 21, 2008

Compiling and Executing Java program

There are three basic steps in development of a simple java program which are Coding, Compiling, and Executing the java program. Coding is to write different set of classes and interfaces using any text editor like notepad etc. There are three cases which a java program can have. Those are:

Case A: Classes in default package (no package) and does not use classes which are bundled in jar.
Case B: Classes which are grouped in different packages but does not use classes which are bundled in jar.
Case C: Classes which are group in different packages and also uses classes bundled in jar.

Now i discuss compilation and execution of java program in each case one by one.

Lets discuss Case A. It is most simplest case which does not have any dependency on classes of others which are bundled in jar. Morever, classes are not grouped in different packages. Classes which are not grouped in packages, are said to be contained in default package. Suppose you have created a folder named as "TestProject" and has placed all java files there, then you can do following in windows to compile and exeuctge java program:
Just go to directory "TestProject" through command prompt and write following command:
C:\TestProject>javac *
above command will compile all java files. Now In order to run the java program, write following command:
C:\TestProject>java MainClass
where MainClass is any class which contains "public static void main(String[] args)" method.

Now we discuss Case B in which classes are grouped under packages. Packages provide better management of code, easy maintenance, and good access control on classes. Morever, packages provide safety from class name collissions. This case has further two possibilities. One is that you have logical grouped java classes in packages but placed them together like in case A i.e. all classes in folder TestProject. Now you will use following command to compile the code:
C:\TestProject>javac * -d .
"d" switch is used to provide destination for compiled classes. When destination is ".", it means root directory (TestProject in this case) will be default directory. javac command will first create folder heirarchy similar to packages and will place compiled class files in respective folder e.g. if a class is in package "org.engineers.project", then javac will first create folder named as "org" at root directory, then will create "engineers" folder under "org" and "project" folder under "engineers" folder. and will place class in "project" folder. In order to execute java program, you have to write following command at root directory:
C:\TestProject>java fully_qualified_class_name
fully_qualified_class_name means name of class with package e.g. if a class "Main" is contained under package org.engineers.project, then fully_qualified_class_name will be "org.engineers.project.Main"

Now we talk about second possibility in Case B which is that you have placed java source files in same folder hierarchy, as of packages i.e. if a class "Main.java" is in package "org.engineers.project", you have placed Main.java in folder org/engineers/project folder. You can use same command to compile and execute java program as in first possibility but it will mix your compiled code with source code so in order to keep them separate create a directory named as "src" under root directory (TestProject in this case) and then place all folder hierarchy containing source files there. Now create another folder named as "classes" at root level and use following command to compile the code:
C:\TestProject>javac * -d classes
"classes" folder be treated as root folder for class files.
In order to run java program, you have to change java command slightly. New command will be:
C:\TestProject>java -cp classes org.engineers.project.Main
Difference with this command and with previous command is of addition switch named as "cp". it represents classpath and tells java command where to find compiled classes. Since compiled classes are in "classes" folder, so i added that folder in classpath.

Now its time to discuss Case C, which is more realistic case. In this case, java source files uses classes which are placed in "jar" file. Jar file is just a zip file but with extensino java archive ("jar") . Now you will have to include all jar files in classpath using same switch which we used in preceding case to run the program. but this time we will have to use switch in both javac and java command.
C:\TestProject> javac -cp test.jar * -d classes
where test.jar is jar file which contains other classes which my classes use.
To execute the java program use following command:
C:\TestProject> java -cp test.jar;classes org.enigneers.project.Main
Note that i have added both "test.jar" and "classes folder in classpath.

4 comments:

Unknown said...

Helpful article, but when I created two folder src and classes inside root. lets say root is testproject as u mentioned, then all java files are present in testproject\src. So if u do javac * -d classes it does not find any java file as they are not in root. I do not think I need to go down to each folder level on command prompt to compile the code... I m new to java ... there cud be a easy solution.. i m not aware off...

amer sohail said...

Well Raj you missed one thing in your command and that is ".", So complete command is:
"javac * -d ."
Try it. If it does not run. tell me.

Sanjeewa said...

When i try the second possibility of Case B i got an error when i try to compile the .java file under the hierarchy of the package.
The error is :
error: Class names, 'classes,src', are only accepted if annotation processing is
explicitly requested
1 error

I have followed every step you told.

Thanks in advance.

Unknown said...

do you know how to execute case #3 if I have a jar file built.