- Java 9:Building Robust Modular Applications
- Dr. Edward Lavieri Peter Verhas Jason Lee
- 497字
- 2025-04-04 17:08:34
Testing a simple Java application
The following code consists of a single Java class, GeneratePassword. This class prompts the user for a desired password length and then generates a password based on the user's requested length. If the user asks for a length shorter than 8, the default length of 8 will be used. This code was written with the Java SE 1.7 JRE System Library:
/*
* This is a simple password generation app
*/
import java.util.Scanner;
public class GeneratePassword
{
public static void main(String[] args)
{
// passwordLength int set up to easily change the schema
int passwordLength = 8; //default value
Scanner in = new Scanner(System.in);
System.out.println("How long would you like your
password (min 8)?");
int desiredLength;
desiredLength = in.nextInt();
// Test user input
if (desiredLength >8)
{
passwordLength = desiredLength;
}
// Generate new password
String newPassword = createNewPassword(passwordLength);
// Prepare and provide output
String output = "nYour new " + passwordLength
+ "-character password is: ";
System.out.println(output + newPassword);
}
public static String createNewPassword(int lengthOfPassword)
{
// Start with an empty String
String newPassword = "";
// Populate password
for (int i = 0; i < lengthOfPassword; i++)
{
newPassword = newPassword + randomizeFromSet(
"aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ
0123456789+-*/?!@#$%&");
}
return newPassword;
}
public static String randomizeFromSet(String characterSet)
{
int len = characterSet.length();
int ran = (int)(len * Math.random());
return characterSet.substring(ran, ran + 1);
}
}
In the following screenshot, we test the GeneratePassword app on a Mac running Java 8. As you can see, we start by querying Java to verify the current version. In this test, Java 1.8.0_121 was used. Next, we compile the GeneratePassword Java file using the javac utility. Lastly, we run the app:

As you can see from the preceding test, GeneratePassword.java was successfully compiled with the GeneratePassword.class file resulting. The application was run using the java GeneratePassword command. The user was prompted for a desired password length and 32 was entered. The application then successfully generated a 32-character random password and provided the appropriate output.
This test demonstrated the example application works successfully using JDK 1.8. Next, let's test the same application using JDK 9.
We start with the java -version command to show that we are using JDK 9 on this computer. The following screenshot shows that we successfully compiled the .java file to a .class file. When the application was run, it functioned as expected and provided the proper results:

As you can see, we clearly demonstrated that a pre-Java 9 application has the potential to successfully run on Java 9 without having to make any modifications. This is a simple case study and featured a very basic Java program. This is, of course, the best case scenario, and cannot be assumed. You will want to test your applications to ensure they run as expected on the Java 9 platform.
In the next section, we will review some potential issues you might encounter when testing your pre-Java 9 applications using the new Java platform with JDK 9.