210 likes | 334 Views
COSC-4840 Software Engineering. Lab Session 1 Introduction to Java and Eclipse Prepared by Nadya Kuzmina Thanks to Lucas Shaw for preparing the examples of the Java section. A Friendly Program. HelloWorldApp .java: [1] //the class needs to have the same name as the file it is in
E N D
COSC-4840 Software Engineering Lab Session 1 Introduction to Java and Eclipse Prepared by Nadya Kuzmina Thanks to Lucas Shaw for preparing the examples of the Java section.
A Friendly Program HelloWorldApp.java:[1] //the class needs to have the same name as the file it is in public class HelloWorldApp { public static void main (String args[]) { System.out.println (“Hello World!”); } }
Java[1] • The Java code in *.java files is compiled into byte code in *.class files • The byte code in the *.class file is interpreted by the Java Virtual Machine (JVM)
Java • Class files provide platform independence for systems that use the same JVM targeted by the compiler used to create it.
A Generally Friendly Program • Let’s make a better Hello World application called HelloApp: • First, make 2 directories: src and classesWindows: Linux:C:\> mkdir src [usr@comp ~]$ mkdir srcC:\> mkdir classes [usr@comp ~]$ mkdir classes • Inside the src directory, make apps and objects directories (these are our package directories):Windows: Linux:C:\> cd src [usr@comp ~]$ cd srcC:\src> mkdir apps [usr@comp ~/src]$ mkdir appsC:\src> mkdir objects [usr@comp ~/src]$ mkdir objects • Now in objects, create HelloObject.java and in apps create HelloApp.java:Windows:C:\src> notepad objects\HelloObject.javaC:\src> notepad apps\HelloApp.javaLinux:[usr@comp ~/src]$ xemacs objects/HelloObject.java apps/HelloApp.java &
A Generally Friendly Program HelloObject.java code: package objects; //HelloObject is in the objects package /** * A very friendly class. * @author Smiley McNiceGuy * @version 1.0 */ public class HelloObject { /** The Object to greet with "Hello!" */ private Object toGreet; /** Default constructor: Initializes toGreet with the String "World". */ public HelloObject () { toGreet = new String ("World"); } /** * Constructor: Initializes the object to greet with "Hello!" * @param toGreet The Object to which to say “Hello”. */ public HelloObject (Object toGreet) { this.toGreet = toGreet; } /** Used by print functions to decide how to output an object. */ public String toString () { return "Hello " + toGreet.toString () + "!"; } }
A Generally Friendly Program HelloApp.java code: package apps; //HelloApp is in the apps package //Import the friendly object. import objects.HelloObject; //We want to use the HelloObject from the objects package public class HelloApp { public static void main (String args []) { //try substituting objects other than String in here! //E.g. HelloObject obj = new HelloObject (new HelloObject ()); - what would this do? HelloObject obj = new HelloObject (new String (“Software Engineering Lab”)); System.out.println (obj); } }
A Generally Friendly Program Compile the HelloApp application: Windows: C:\src> javac –d ..\classes objects\HelloObject.java apps\HelloApp.java Linux: [user@comp ~\src]$ javac –d ../classes objects/HelloObject.java apps/HelloApp.java Run the HelloApp application: Windows: C:\src> cd ..\classes C:\classes> java –cp . apps.HelloApp Linux: [user@comp ~\src]$ cd ../classes [user@comp ~\classes]$ java –cp . apps.HelloApp
Java Memo: • JVM uses a classpath to find classes: all the classes you use must be in the classpath, otherwise a ClassDefNotFoundException will be thrown. • A class must be contained in a file with the same name as the class, e.g. • Public class Hello { … } must be in Hello.java • Every class belongs to some package. A class belongs to the default package if the package is not specified explicitly. It is good style to explicitly specify the package for a class. The current naming convention is to start with a top-level domain name and work your way down. For example, the class “Hello” may be in the package edu.uwyo.cs.simple. • Code conventions for Java are available at: http://java.sun.com/docs/codeconv/ • When invoked on a class, JVM starts by executing public static void main (String[] args) method.
What is Eclipse? [2] • Eclipse is an open source community focused on developing a universal platform of frameworks and exemplary tools that make it easy and cost-effective to build and deploy software. • Java™ technology developers using Eclipse benefit from: • A world-class Java IDE • Native look and feel across platforms • Easy extensions to Java tooling
Installing Eclipse • Open “Eclipse for Java and Web Developers” page: http://www.eclipse.org/callisto/java.php • Follow the “Eclipse SDK 3.2” link to Eclipse downloads. • Download the appropriate Eclipse SDK (it comes in an archive). • Unpack the archive. • Your installation is complete! • The unpacked directory eclipse/ contains the Eclipse executable.
Starting Eclipse • Is as simple as clicking on the Eclipse executable in the freshly unpacked Eclipse directory. • On startup, Eclipse will ask you about the workspace directory. Eclipse will store all your projects under the directory you will specify in this dialog. Choose a convenient directory for storing your projects.
Getting Started with a Java Project in Eclipse • File -> New -> Project • Select “Java Project” in the “Select Wizard” dialog box. • Enter the name for the project (e.g. “hello”) • The next dialog displays the project that you have created. It is the right place to specify your custom source and output directory. Eclipse will output the *.class files into the output directory (Described on the next slide).
Specifying custom directories for source files and class files. • Assuming that you are still in the “Java Settings” dialog box, • To specify a directory called “src” for your source files click on “Details” and choose the “Create new source folder” link. Enter “src” in the textfield that appeared and choose “Finish”. • To specify a directory called “build” for your class files, look for “Default output folder” in the source tab of the “Java Settings” dialog box. Click on the Browse button beside the textbox which displays the current setting for the output folder (<project-name>/bin by default). • Select the top folder of your project (or the folder that should contain your custom output directory). • Choose Create New Folder in the appeared dialog box. • Enter “build” in the appeared textbox and press “OK”. • Press “OK” in the “Folder Selection” dialog box. • Press “Finish” on the “Java Settings” dialog box. • Your new project has been created!
Working with your New Project • Every Java class belongs in a package. • Start by creating a new package in your project: right-click on the project’s name and choose “New -> Package”. • Populate your package with classes. • Right-click in the package name and choose “New -> Class”. Enter the name of the class and select the main method stub if you want to run the class you are creating.
Running a Class • You can run any class containing the main method. • Right click the name of the class on the left and choose “Run As -> Java Application”. • Note: you may need to fix the compilation errors before your application will run. • You are on your way! Good luck!
Eclipse Resources: • Home of the Eclipse project: http://www.eclipse.org/ • Eclipse movie tutorials on: http://www.eclipse.org/callisto/java.php • Eclipse Help User Guides (go to menu Help -> Help Contents). • IBM developerWorks: http://www-128.ibm.com/developerworks/opensource/top-projects/eclipse-starthere.html • Article “A Look at the Eclipse Callisto Release ”: http://java.sys-con.com/read/250202.htm
Java Resources: • The Java Tutorial (an excellent way to get started with Java): http://java.sun.com/docs/books/tutorial/reallybigindex.html • Good Java Articles: www.onjava.com www.javaworld.com • The list of Java Resources on Sun’s web site: http://java.sun.com/docs/books/tutorial/information/resources.html
References [1] The Java Tutorials. Trail: Getting Started. Lesson: The Java Technology Phenomenon. http://java.sun.com/docs/books/tutorial/getStarted/intro/index.html [2] Get started now with Eclipse http://www-128.ibm.com/developerworks/opensource/top-projects/eclipse-starthere.html