September 15, 2009

How to set up environment to work with Java

Following post consist of setting up the environment for writing a Simple Java Program

1. Install jdk1.6 (latest version) on your system. (If any issues in installation, do ask here). Look at the following link for downloads Java SE Downloads - Sun Developer Network (SDN)

2. Create a source folder in your system where all your Java programs will be stored (let it be C:\JavaSrcCode..This is my system made folder. I would recommed you all to make following folder structure D:\MyJava\source)

3. Create Example.java file in the above created folder(My is JavaSrcCode, your will be 'source' folder) Example.java (Details of this file is discussed in next post ) [CODE] public class Example{ public static void main(String args[]){ System.out.println("Hello Example Test"); } } [/CODE] 4. Now for compiling from command prompt a)Switch your location to bin directory of installed location of your java(C:\Program Files\Java\jdk1.6.0_02\bin) b)Now from that location type "javac " (eg javac C:\JavaSrcCode\Example.java) See following figure Above command successfully generates the Example.class file for Example.java file in C:\JavaSrcCode folder only where Example.java file is stored. 5. Now for running the class file.Issue the following command from command prompt java -classpath e.g. java -classpath C:\JavaSrcCode Example as shown in following diagram -classpath : is just nothing but the list of directories in which classes might be found e.g in above case C:\JavaSrcCode is used to tell the JVM to look for .class file in specified folder. While running the file , there is no need to give Example.java (because while running JVM takes .class file as input) So above commands in the diagram will successfully run the program showing the output. [ Note :There is one more method of making compiling and running of Java programs easier. Don't need to make Example.java file in any folder. Simply make it in bin folder of installed java in your system. Then while compiling the Example.java, a) Switch your location to bin directory of installed location of your java(C:\Program Files\Java\jdk1.6.0_02\bin) b) command will be simply javac Example.java and while running command will be simple java Example But we should avoid using this approach of using Java. ] One more easier way is to use Java IDE. If any of you can get Netbeans version 6 or Eclipse(freeware) or Gel. Then it will be well and good. It will reduce your effort in running and compiling java programs through Command prompt.


[QUOTE=shalini_goel14;42353][Day1 Class Contd..] If anyone here wants me to put some light on object-oriented concepts. Do let me know. As it is theoretical concept so I am not going into it here ok. So now coming to writing a simple basic Program in java : I will consider the program written in above post [code] public class Example{ public static void main(String args[]){ System.out.println("Hello Example Test"); } }[/code] A simple class program in Java represents a generic thing for any real world object Now for writing a Java Program 1. Open notepad (if not using Java IDE) and save the file with .java extension in source folder (the one we created in step 2 of last post) Let me name that file as Example.java 2. Now add following lines of code for creating the class public class Example { } [Note: 1. Always remember the Java class name should match with that of the file name for example in above post case. The file name was Example.java which was exactly same as class name in code(public class Example). 2. Java is case-sensitive language. ] So now my Java class is defined. 3. Now add following lines: public static void main(String args[]){ } Look carefully here Above lines of code is much similar like creating a method of name main Here public means this method is accessible from anywhere outside the class. As main method here is accessible as it must be called by code outside of its class when program is started. Now keyword static allows main() to be called without having to instantiate a particular instance of the class (Will teach in further lectures how to instantiate a class). Actually this is infact necessary since main() is called by JVM before any objects are made. The keyword void simply tells the compiler that main() does not return a value. Any information that we need to pass to the method is received by the variables(called as parameters) specified within the set of parentheses followed by the name of the method. So in above main() method there is only one parameter named as args which is an array of instances of the class String.Here args receives any command-line arguments present when the program is executed. { and } shows the main method's body start and end. 4. Next add the following line of code in main() method's body System.out.println("Hello Example Test"); Here System is a predefined class that provides access to the system and out is the output stream that is connected to the console.println() displays the string which is passed to it. Notice that println() command end with a semicolon ; . So with all this I take leave from this thread and hope you all would have enjoyed the first day with Java. [Assignment: Setting up the environment on your system and writing a simple Java program that will print "Hello World " in output. ][/QUOTE]