1 / 11

Java Applications Development: A Short Tutorial

Java, over all these years, had been emerging as the best object-oriented programming language supporting the creation and compilation of the most successful applications. The world had hence not been unknown from Java applications development.

Lesson
Download Presentation

Java Applications Development: A Short Tutorial

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Applications Development: A Short Tutorial Java, over all these years, had been emerging as the best object-oriented programming language supporting the creation and compilation of the most successful applications. The world had hence not been unknown from Java applications development. Most of the web applications seem to be driven by the fact that Java holds on to the best technologies that can maintain pace with the changing forms of how worldwide population is getting connected. Well now, when Java had been such an important element for application development, we are going to get through some short tutorials and processes of application development with Java. Java development services avail you with a lot of usable stuff in order to design up-to-the-mark apps. However, knowing the entire procedure well would help you bring up some of your own projects as a budding business professional in an even easy manner. If not projects, you may also try for some creative tasks being a student. Anyway, this is going to be interesting if you are just a programming mind! General Java Applications Development

  2. The following is a short tutorial which would guide you across the most appropriate steps for developing a Java SE application with NetBeans. We would begin this tutorial assuming that you have already some familiarity with Java and its basics. Along the way that comes up, you may also be able to get through and learn some more IDE features. These features would have an important role to play in the simplification of the app development process. Now, here we would be creating an application that can convert several different words into a single word, this is known as an acrostic. Seems to be interesting right? This 30 minutes tutorial will let you learn it well. You will Need… Following resources would be required for successfully completing the project: Software/Resource Version Java Development Kit (JDK) 6, 7 or 8 NetBeans IDE 7.4 or 8.0 Project Setup This Project will focus on how to create application in Java using NetBeans. The application we are going to create would contain two projects. 1.A Java Class Library which will include creating a Utility Class. 2.A Java Application Project; this will consist of the main class that will implement the method from the utility class in Library. Once you’ve created the projects, you will have to add the same to the classpath of the application project. Coding of the application will then follow. In the Library project, you will introduce a utility class with the acrostic method.

  3. The acrostic will consider a given array of words as its parameter; it would then generate a meaningful word based on those words. The main class would be in the MyApp Project and would further call the acrostic method, and passes the words which would be entered when the application is run. You may understand better through this video: NOTE: Two projects are really not needed for an application too simple. The tutorial, however, demonstrates two so that the features used in the entire process are clearly understood to be further used in a complex application than this. ALSO READ: UNDERSTANDING ADVANCED PHP TECHNIQUES: RARELY USED TRICKS Create a Java Class Library Project Follow the below-mentioned steps for this: 1.Choose File > Select New Project OR Press Ctrl+Shift+N. Select Java under Categories and Java Class Library under Projects. Click Next. 2.Type MyLib under Project Name. Now change the Project Location within any directory on your computer. Suppose this directory to be NetBeansProjects. This is how we would refer it further in the tutorial. NOTE: The above-mentioned path would appear in the Project Folder Field as: /NetBeansProjects/MyLib. 3.Select Use Dedicate Folder for Storing Libraries checkbox; now specify the location of Libraries folder. (This is optional) 4.Click Finish. The MyLib Project will now open in the Projects and Files window together. Create a Java Application Project Follow the steps mentioned below: Choose File > Select New Project OR Press Ctrl+Shift+N. Select Java under Categories and under Projects, choose Java Application. Click Next. Type MyApp under project name. Do not forget to set the Project Location to NetBeansProjects.

  4. Check Use Dedicated Folder for Storing Libraries checkbox (This is optional). Enter the main class as Main. Make sure to check the Create Main Class checkbox. Click Finish. The MyApp Project will display in the Project window. Java will open in the Source Editor. Configuring the Compilation Class Path MyLib needs to be added to the classpath of MyApp since it would be dependent on a class in MyLib. This is also important with a view to ensure that classes in MyApp Project can easily refer the classes in MyLib and no compilation errors are caused. This will also let you complete codes in MyApp that are based on MyLib. The classpath would be visually represented as the Libraries node in the IDE. Adding Utility Classes from Library to the Project Classpath Right click on the Libraries node in the Projects window and choose Add Project in MyApp. Browse NetBeansProjects > Select MyLib project folder. JAR Files that can be added to the project are displayed on the Project JAR Files pane. There would be a JAR file for MyLib even if you have not built it yet. When you build and run the MyApp Project, this JAR file will be built. Click on Add Project JAR Files. Expand Libraries node. The JAR file of MyLib Project is added to the Classpath of MyApp Project. Create and Edit Java Source Code Now, a Java package needs to be created and the method that would be used to construct the acrostic. After that, you need to look forward to implementing the acrostic method in Main class. Creating Java Package and Class File Consider the following steps for this:

  5. Right-click MyLib project > choose New> Java Class. Type the name for the new class as LibClass. In the Packaged field type me.mylib > click Finish. In the Source Editor LibClass.Java will open. Place the cursor on the line in LibClass.java after class declaration (public class LibClass()). Now, type in the following code: public static String acrostic(String[] args) { StringBuffer b = new StringBuffer(); for (int i = 0; i < args.length; i++) { if (args[i].length() > i) { b.append(args[i].charAt(i)); } else { b.append('?'); } } return b.toString(); } If the code that you paste is not properly formatted, press Alt+Shift+F and reformat the file. Save the file. Editing a Java File You will now have to add some code to Main.java. While doing so you would get to know and use the features of Source Editor’s code template and code completion. Get on to the following steps: In the Source Editor, select the javatab. If it’s not open already, expand MyApp > Source Packages > acrostic in the Projects window now double- click Main.java. Delete the comment in the main method which mentions // TODO code application logic here. Type the following code in place of the comment: String result = Li

  6. Immediately after Li leave the cursor, you will further make use of code completion turning Li into LibClass. To open the code completion box, press Ctrl + Space. On the screen would be a short list of possible ways for completing the word. However, LibClass which you actually need might not be in that list. For a longer list of possible matches press Ctrl + Space once again. The list which appears now would contain LibClass. Select LibClass and press Enter. The rest of the class name would be filled by IDE and an import statement would be automatically created. Note: IDE would also open up a box above the code completion box displaying Javadoc information for the specific package or class. However, for this package since there is no Javadoc information, a message stating “Cannot find Javadoc” will be displayed. After LibClass, type a period (.) in the main method. The code completion box will open again. Select acrostic(String[]args) method and Press Enter. The acrostic method would be filled in by IDE and the args parameter would be highlighted. To accept args as the parameter, Press Enter. Now type a semicolon (;). The final line should look like the following: String result = LibClass.acrostic(args); To start a new line, press Enter. Now type sout and then press Tab. When expanded the sout abbreviation is out.printIn(“ “); Now, place the cursor between the quotation marks and type Result = and + result inside and after the quotation marks respectively. The final line should appear like the following: System.out.println("Result = " + result); Press Ctrl + S, and save the file. Note: sout is one of the many code templates available in the Source Editor. In order to find and edit the list of code templates do as follows: Choose Tools > Options > Editor > Code Template. Compiling and Running the Application

  7. To be able to run the project you will now need to set the main class and execution arguments. Note: By default, the Compile on Save feature is enabled and with that, the projects have been created. Therefore, to run the application in IDE you will not need to compile your code first. Setting the Main Class and Execution Arguments The output of the program is dependent on the arguments you give while running it. You will have to provide five words as argument out of which an acrostic “Hello” would be generated. The acrostic would be assembled considering the first letter of the first word, the second letter of the second, third letter for the next word and accordingly further. To add arguments to IDE when the application is run: Right-click on the MyApp project node, select Properties > in the dialog’s left pane, select Run node. Remember to set the main class to Main. In the Arguments field, type something, say, However, we all feel zealous and press OK. Running the Application Once you have created the application and provided the runtime arguments, you can now test run the application in IDE. To run the application in IDE, follow the given steps: Right-click on the MyApp project node > choose Clean and Build. Choose Run > Run Project (F6). In the Output window, the output of the program should look like: Result = Hello i.e. the acrostic phrase that was passed as an argument to the program. Testing and Debugging the Application

  8. Now, you will need to create and run a test for the project using JUnit; run the application in IDE’s debugger, and check for errors if any. In the JUnit test, by passing a phrase to the acrostic method, you will test the LibClass with an assertion to indicate what the probable result should be. Creating JUnits Tests In the Projects window, Right-click the LibClass.java node. Now, choose Tools > Update/Create Tests (Ctrl + Shift + U). Click OK in the Create Tests dialog box to run the command with default options and settings. Note: If you have created the JUnit tests in the IDE for the first time, the Select JUnit Version Dialog box would prompt before you. Select JUnit 4.x, Press Enter and continue with the Create Tests dialog box. The org.me.mylib package and LibClassTest.java file would be created by the IDE in a separate test folder. By expanding the org.me.mylib subnode and Test Packages node, you can find this file. Delete the body of the public void testAcrostic() method in LibClassTest.java. Type the following text in place of the lines deleted: System.err.println("Running testAcrostic..."); String result = LibClass.acrostic(new String[] {"fnord", "polly", "tropism"}); assertEquals("Correct value", "foo", result); Press Ctrl + S and Save the file. ALSO READ: BIG DATA SECURITY: SECURITY ISSUES AND CHALLENGES IN THE QUEUE Running JUnit Tests Select MyLib project node, choose Run > Test Project (MyLib) or press Alt + F6. In the Output window, MyLib (test) tab will open. The JUnit test cases will be now compiled and run; the test results will show all tests passed. Rather than testing the entire project, you may also choose to run a single test file. In the Source Editor, select the java tab, choose Run > Test File.

  9. The JUnit API documentation would be available from the IDE. Further, Choose Help > Javadoc References > JUnit API. Note: If you are accessing Javadoc in the IDE for the first time then first you need to Choose Help > Javadoc References > More Javadoc. Now, Click Cancel in the Javadoc References dialog box. Further, Choose Help > Javadoc References > JUnit API. Debugging the Application Here, you will make use of the debugger set through the application and watch the changing values of the variables as the acrostic is assembled. For running the application in debugger follow the steps below: 1.In the file, java go to acrostic method. Anywhere inside b.append(args[i].charAt(i)); place the insertion pointer. Now, to set a breakpoint, Press Ctrl + F8. 2.Select MyApp project node > choose Debug > Debug Project (Ctrl + F5). The Debugger Window is opened by the IDE and the project is run in the debugger until the breakpoint. 3.In the bottom of IDE select the Local Variables Window and expand the args node. The array of string carries the phrase that had been entered as a command argument. 4.Choose Debug > Step Into OR Press F7. This will let you step through the program and watch the changing value of the b variable as the acrostic is formed. As the program reaches the end the debugger window closes. Building, Running And Distributing the Application Once you are satisfied with the working of the application you may start preparing to deploy the application outside the IDE. Now you will have to consider building the JAR file for the application and running the same from the command line. Building the Application The Clean and Build command is the main build command in the IDE. The compiled classes and other build artifacts are deleted by the command and then the entire project is rebuilt from scratch.

  10. Note: There is also a build command which will not delete the old build artifacts. However, this command is known to be disabled by default. To build the application, follow the given steps: Choose Run > Press Shift + F11 or Choose Clean and Build Project. In the Output window, the Output from Ant build script appears. If the Output window doesn’t appear you may also open it manually. To do this, choose Window > Output > Output. When you clean and build the project, following things would occur: Output folders from the previous build actions are deleted (“cleaned”). (These are mostly dist and build folders). Hereafter, the dist and build folders are added to your project, the folder will now be referred as PROJECT_HOME Folder. These folders can be viewed in the Files Window. All the sources are then compiled to .class files, which and placed in the PROJECT_HOME/build folder. Inside the folder, PROJECT_HOME/dist, a JAR File is created which contains your project. In case you have specified any libraries for the project apart from the JDK, in the dist folder a lib folder is created and the libraries are further copied to dist/lib. The manifest file in the JAR is updated with the entries designating the main class and libraries on the project’s classpath. Note: The contents of the manifest can be viewed in the IDE’s File window. Once you’ve built the project, switch to the files window > dist/MyApp.jar. Expand the nodes for JAR file > Expand META-INF folder. Now, to display the manifest in Source Editor, double-click MANIFEST.MF. Main-Class: acrostic.Main Class-Path: lib/MyLib.jar Running the Application outside IDE Follow the steps in order to run the application outside IDE: 1.Open command prompt or terminal window on your system. 2.Change directories to MyApp/dist directory in command prompt.

  11. 3.At the command line, type the following: java -jar MyApp.jar However we all feel zealous The application will then execute and return the output. Result = Hello Distributing the Application When you have finally verified the workability of the application outside the IDE, you may now look on for distributing the application. Follow the given steps for the purpose: 1.Create a zip file containing the application JAR file on your system, it should also be accompanied by the lib folder and jar. 2.Send the file to the people who would be using the application. Tell them to unpack the zip file, also make sure that the lib folder and the MyApp.jar file are in the same folder. 3.Instruct them to follow the required steps for running the application. And it’s done. This tutorial would hopefully help you to clear the several concepts of Java applications development.

More Related