You are on page 1of 3

Java Programming

5-3: Deploying an Application


Practice Solutions

Lesson Objectives:
• Describe the concept of packages
• Describe how to deploy an application
• Describe a complete Java application that includes a database back end

Vocabulary:
Identify the vocabulary word for each definition below.

Deployment The process of getting your program ready and available for any user to run the program
independent of an IDE.

JNLP files Files that describes how your application/applet should be launched.

HTML files Files can be displayed in a web browser as web pages.

Two-tier Architecture A java system with a client and a server.

Three-tier Architecture A java system with a client, a server, and a database.

Package A collection of java classes organized together.

JAR files A java version of zip files.

Try It/Solve It:

1. Take the following java code and place it into a package named greeting in Eclipse:
public class MyName {
private String name = "Jane";

public String getName() {


return name;
}//end method getName

public void setName(String name) {


this.name = name;
}//end method setName

public void sayHello() {


System.out.println("Hi" + this.name);
}//end method sayHello
}//end class MyName

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.
Answer:

package greeting;

public class MyName {


private String name = "Jane";

public String getName() {


return name;
}//end method getName

public void setName(String name) {


this.name = name;
}//end method setName

public void sayHello() {


System.out.println("Hi" + this.name);
}//end method sayHello
}//end class MyName

2. Create a new package called converse in the same project and then create a class called Hello. You need to use the sayHello
method from your MyName class.

a) What do you need to include in your java program to get this to work?
Answer:
import greeting.*;

b) Show your completed code.


Example:
package converse;

import greeting.*;

public class Hello {

public static void main(String[] args) {


MyName myName = new MyName();
myName.sayHello();
}//end of method main
}//end of class Hello

3. You want to be able to change the name and then display the new name from within the Hello class.

a) How could you call the setName in a program before displaying the new name (Bob) to screen without using an import
statement for the package?
Answer:
greeting.MyName myName = new greeting.MyName();
myName.setName("Bob");

b) Show your completed code.


Example:
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

2
package converse;

public class Hello {

public static void main(String[] args) {


// TODO Auto-generated method stub
greeting.MyName myName = new greeting.MyName();
myName.setName("Bob");
myName.sayHello();
}//end of method main
}//end of class Hello

4. Using the following example create a runnable JAR file using Eclipse.
Example Steps
a) In Eclipse create a new Java Project named dice.
b) In this project create a package also named dice.
c) In this package create a class named Random.
d) Add the following code to the Random class:
package dice;

import javax.swing.JOptionPane;

public class Random {

public static void main(String[] args) {


int rollOfDice;
String output;

// generate a random number between 1 and 6 inclusive


rollOfDice = (int) (Math.random() * 6) + 1;
//create a String message for the output window
output = "You rolled a " + rollOfDice;

// print message using a window


JOptionPane.showMessageDialog(null, output, "Random Number Demo",
JOptionPane.INFORMATION_MESSAGE);

System.exit(0);
}//end method main
}//end class Random

e) Save and run your code.


f) Now create a runnable JAR file and test that it runs outside the IDE!
g) Detail the steps taken to create the runnable JAR file!

Answer:
• Right click the dice project file folder on the project explorer window and choose Export from the menu.
• From the dialog box open the Java option and then choose runnable JAR file from the list.
• Click on the next button.
• On the screen configure the options as you want.
• Go to your save location and double click the JAR file.
• Your program should run.
Copyright © 2020, Oracle and/or its affiliates. All rights reserved. Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners.

You might also like