Java Reference
In-Depth Information
This may seem like a lot of work (for very little result). However, most of the work was one time, setup tasks.
To rerun the application you only have to execute the java command again. No environment variables have to be
reset, nor do you have to export the application again. However, if the application were modified, the classes would
have to be reexported.
Tutorial: Breaking the Code
The batch file is very important to the successful installation of a Java application. Unfortunately, batch file mistakes
are not easily diagnosed. We will create some of the most common mistakes and show their effects on running the
application.
1.
Start a Command Prompt session.
2.
Start Notepad and open Employee_Application.bat.
3.
On the first line change path=f:\jre\bin to path=f:\jre\Bin (i.e., change the b
to upper case).
4.
Save the source code.
5.
At the command prompt, type f:\Employee_Application and press Enter.
The application worked! You are probably saying, “Hey, you said Java was case sensitive!” That's still true.
However, Windows is not case-sensitive. If you reference files called Employee, employee, or EMPLOYEE, Windows
will find the same file.
6.
On the second line change the f to c (i.e., change the classpath).
7.
Save the source code, go to the command prompt, type f:\Employee_Application and
press Enter.
You will get the following (always frustrating) error message:
Exception in thread “main” java.lang.NoClassDefFoundError: myFirstPackage/EmployeeApp
The error was caused by an incorrect classpath. Let's walk through what is happening. The java command tells
the JVM to execute myFirstPackage/EmployeeApp. The JVM searches through all the folders in the classpath (C:)
looking for the folder called myFirstPackage. Unfortunately, myFirstPackage is on the F: not the C:. (Note, the set
classpath command was executed, Windows not only did not check to see C: had the class file, it doesn't even check
to see if the C: is a valid drive.) Because the application cannot be found, the JVM throws up its hands and issues the
“No class found” message. This is a frustrating error to fix because the message does not provide any clues as to where
the problem is. For example, this message is also generated if the application name is specified incorrectly in the java
command. Let's prove it.
8.
On the second line change C: back to F:.
9.
On the third line, delete the last p (i.e., change EmployeeApp to EmployeeAp).
10.
Save the source code.
11.
At the command prompt, type f:\Employee_Application and press Enter.
Notice that the same error is generated, but the message says it cannot find EmployeeAp. Whenever you get the
“No class found” message, try to remember that the JVM usually cannot find the class specified in the java command
because of a simple typo in either the classpath or the application name.
12.
On the third line, change EmployeeAp back to EmployeeApp.
 
Search WWH ::




Custom Search