Customized Java EE Training: http://courses.coreservlets.com/
J ava 7, J ava 8, J SF 2.2, PrimeFaces, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Java with Eclipse: Setup & Getting Started Originals of slides and source code for examples: http://courses.coreservlets.com/Course-Materials/java.html Also see the Java 8 tutorial http://www.coreservlets.com/java-8-tutorial/ and customized Java training courses (onsite or at public venues) http://courses.coreservlets.com/java-training.html 2014 Marty Hall Customized Java EE Training: http://courses.coreservlets.com/ J ava 7, J ava 8, J SF 2.2, PrimeFaces, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Taught by lead author of Core Servlets & J SP, co-author of Core J SF (4 th Ed), & this tutorial. Available at public venues, or customized versions can be held on-site at your organization. Courses developed and taught by Marty Hall J SF 2.2, PrimeFaces, servlets/J SP, Ajax, jQuery, Android development, J ava 7 or 8 programming, custom mix of topics Courses available in any state or country. Maryland/DC area companies can also choose afternoon/evening courses. Courses developed and taught by coreservlets.com experts (edited by Marty) Spring, Hibernate/J PA, GWT, Hadoop, HTML5, RESTful Web Services Contact hall@coreservlets.com for details For live Java-related training, email hall@coreservlets.com Marty is also available for consulting and development support Topics in This Section Installing Java Installing and configuring Eclipse Importing sample projects Executing simple desktop programs Executing applets Eclipse shortcuts 2014 Marty Hall Customized Java EE Training: http://courses.coreservlets.com/ J ava 7, J ava 8, J SF 2.2, PrimeFaces, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Installing Java Java Versions Java SE (Standard Edition) Core J ava J ava version used in this course Latest is J ava 8; J ava 7 or 8 should be used for almost all new projects. J ava 8 was final in March 2014. This tutorial refers to changes in J ava 8, but the J ava-8-specific topics are covered in http://www.coreservlets.com/java-8-tutorial/. Naming conventions are confusing J ava 8 ==J DK 1.8 J ava 7 ==J DK 1.7 J ava 6 ==J DK 1.6 J ava 5 ==J DK 1.5 J ava 2, version 1.4 ==J DK 1.4 Java EE (Enterprise Edition) Same core language, but adds in many libraries for Web apps and other enterprise tasks Many or most real-life deployments start with J ava SE and then get a server that is bundled with the needed J ava EE libraries Features of Recent Java Versions Java 5 Major update. Generics, varargs, printf, @Override, new for loop. Java 6 Minor update. Updates to collections, Swing, etc. Java 7 Minor update. Diamond operator, Strings in switch statements, try-with-resources, updates to Swing (especially new look and feel). Java 8 Major update. Lambdas for functional programming. Streams for bulk operations. Final version March 2014. See tutorial at http://www.coreservlets.com/java-8-tutorial/ Which Java SE Version Should You Use? Server-side applications Use the latest J ava version that your app server supports J DK 1.4 1.8, depending how old your server is If youcan choose, use J DK 1.8 (but 1.7 very common) Desktop apps For best power and speed, use J ava 8 (aka J DK 1.8) Use older only if project is old (but consider upgrading) Android phone apps As of 2014, Google supports only J ava 6 (aka J DK 1.6) Browser apps (Applets) [rare] Separate J ava Plug-In is required in all recent browsers For intranet apps, use J ava 8 On internet, most J ava applet sites (e.g., Yahoo! Games) use J DK 1.6 or 1.7, but all require end-user installation Installing Java SE (Standard Edition) Install Java http://www.oracle.com/technetwork/java/javase/downloads/ Bookmark the Java API ( JavaDocs ) http://docs.oracle.com/javase/8/docs/api/ http://docs.oracle.com/javase/7/docs/api/ This is the most important J ava reference for developers. Eclipse integrates this API, but a separate link is still good Use this version. The JDK Java Development Kit includes compiler for .java files, whereas the JRE Java Runtime Environment is only for executing prebuilt .class files. This tutorial uses Eclipse, but if you prefer the NetBeans environment, it is very easy to adapt the instructions to that development environment. So, if you prefer NetBeans or your organization has standardized on it, use this download instead of (not in addition to) the one below. 2014 Marty Hall Customized Java EE Training: http://courses.coreservlets.com/ J ava 7, J ava 8, J SF 2.2, PrimeFaces, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Installing Eclipse Installing Eclipse Overview Eclipse is a free open source IDE. Support for J ava, Android, HTML, CSS, J avaScript, C++, PHP, J SF, servlets, and more. http://eclipse.org/downloads/ Choose Eclipse IDE for J ava EE Developers Use latest (Kepler early 2014, Luna late 2014). Features Checks your syntax as you type Automatically compiles every time you save file Many tools: refactoring, debugging, server integration, templates for common tasks, etc. Low learning curve: beginners can use Eclipse without knowing these tools Running Eclipse Unzip the downloaded file (no installer!) Call the folder you unzip into installDir Double click eclipse.exe (Mac/Linux similar) From installDir/bin Pic is for Windows, but Mac and Linux is similar Click on Workbench icon Next time you bring up Eclipse, it will come up in workbench automatically Shortcut Many developers put Eclipse link on their desktop R-click eclipse.exe, Copy, then go to desktop, R-click, and Paste Shortcut (not just Paste!) Eclipse: Running Programs Executing program from existing project Open existing project Double click J ava file to bring it up in editor R-click anywhere in code Select Run As J ava Application Output goes in Console at bottom Note: Class must have a main method this is explained in the upcoming basic syntax section Eclipse: Making Projects Creating new project File New Project J ava J ava Project Pick any name To simplify applets later, choose Sources/Classes in project folder Creating new class R-click New Class You can have Eclipse make main when class created, but easier to use shortcut to insert it later Eventually you will make package (subdirectory) first, then put class there Can also copy/rename existing class 2014 Marty Hall Customized Java EE Training: http://courses.coreservlets.com/ J ava 7, J ava 8, J SF 2.2, PrimeFaces, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Building Java Manually (Rare) Creating and Running Program Create the File Write and save a file (say Test.java) that defines public class Test Other than real J ava IDEs (e.g., Eclipse, NetBeans, Intellij IDEA), there are a number of text editors (e.g., TextPad, UltraEdit, vi, emacs) with good J ava support. File and class names are case sensitive Compile the program Compile Test.java > javac Test.java This step creates a file called Test.class Run the program > java Test This step assumes your class has main method 2014 Marty Hall Customized Java EE Training: http://courses.coreservlets.com/ J ava 7, J ava 8, J SF 2.2, PrimeFaces, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Simple Examples Installing Sample Projects Code from all tutorials is available online http://courses.coreservlets.com/ Click on J ava tutorial on top left of page Import project into Eclipse Click on appropriate tutorial section Download ZIP file The one for this section is called intro Start Eclipse and go to Workbench File Import General Existing Projects into Workspace Select archive file (not Select root directory). Then browse to ZIP file you downloaded, OK, Finish Basic Hello World Application File HelloWorld.java: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world."); } } Notes Application is lingo for a stand-alone J ava program An application is a J ava class that contains main Most J ava classes do not contain main, but only those that contain main can be directly executed Executing HelloWorld In Eclipse (recommended) Compiling Done automatically whenever you save a file Executing R-click inside window, then Run As J ava Application Output (see Console tab at bottom) Hello, World Manually (rare) Compiling javac HelloWorld.java Executing java HelloWorld Output Hello, World Basic Hello WWW Applet File HelloWWW.java: import java.applet.Applet; import java.awt.*; public class HelloWWW extends Applet { public void init() { setBackground(Color.BLACK); setForeground(Color.WHITE); setFont(new Font("SansSerif", Font.BOLD, 30)); } public void paint(Graphics g) { g.drawString("Hello, World Wide Web.", 5, 35); } } Basic Hello WWW Applet (Continued) File HelloWWW.html: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>HelloWWW: Simple Applet Test.</title> </head> <body> <h1>HelloWWW: Simple Applet Test.</h1> <p> <applet code="HelloWWW.class" width="460" height="50"> <b>Error! Must have Java enabled in your browser.</b> </applet> </p> </body></html> Basic Hello WWW Applet (Continued) Compiling Eclipse: save the file Manual: javac HelloWWW.java Running: Load HelloWWW.html in a J ava-enabled browser With Eclipse, just drag the file onto browser 2014 Marty Hall Customized Java EE Training: http://courses.coreservlets.com/ J ava 7, J ava 8, J SF 2.2, PrimeFaces, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Some Eclipse Shortcuts A Few Eclipse Tricks Making a new project File New Project J ava J ava Project Choose Source/Classes in Same Folder Making a new class R-click project, New Class Autocompletion Type part of a class or method name, Control-Space Inserting main method Type the word main, then Control-Space Inserting System.out.println Type the word sysout, then Control-Space Renaming a class, variable, or method Select class, variable, or method, R-click, Refactor Rename Will also change all places that refer to it 2014 Marty Hall Customized Java EE Training: http://courses.coreservlets.com/ J ava 7, J ava 8, J SF 2.2, PrimeFaces, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Wrap-Up Summary Downloading Java http://www.oracle.com/technetwork/java/javase/downloads/ Bookmarking the Java API http://docs.oracle.com/javase/8/docs/api/ (or /7/) Downloading Eclipse http://eclipse.org/downloads/ Downloading sample projects http://courses.coreservlets.com/ Click on J ava Programming tutorial on top left Import with File Import Existing Projects Executing a class that has main R-click in code, Run As J ava Application 2014 Marty Hall Customized Java EE Training: http://courses.coreservlets.com/ J ava 7, J ava 8, J SF 2.2, PrimeFaces, J SP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location. Questions? More info: http://courses.coreservlets.com/Course-Materials/java.html General Javaprogrammingtutorial http://www.coreservlets.com/java-8-tutorial/ Java8tutorial http://courses.coreservlets.com/java-training.html CustomizedJavatrainingcourses, at public venues or onsiteat your organization http://coreservlets.com/ JSF2, PrimeFaces, Java7or 8, Ajax, jQuery, Hadoop, RESTful WebServices, Android, HTML5, Spring, Hibernate, Servlets, JSP, GWT, andother JavaEEtraining