10.09.2011

Understand The Java Classpath

This description is about a small easy range of batch files which can be used to test the behavior of the javac compiler and java class launcher for separate classpath scenarios. The system is called ClasspathTester. Download instructions are included at the end of this article.

When you are in the middle of trying to debug a java build it is a bad time to try to boil everything down to a easy test case, so basically the intent of my test range is to do this for you ahead of time. If you can't find the definite test you need, it is easy to modify the existing tests or originate a new test to suit your needs.

Computer Software Classes

How ClasspathTester Works

As previously stated, I did everything with batch files, because I wanted the tests to be as uncomplicated, conveyable (within the Windows environment), and easy as possible. There are a few helper batch files to do things like originate formatted production and build a directory structure, but otherwise nearly all of the work is done in the RunAllTests.bat file.

What RunAllTests.bat consists of is tests and sub-tests. If you haven't downloaded it yet, I recommend you download it and take a look. Each test consists of some sort of a setup. Setup commonly involves the following:

1. Test environment is cleaned up of old directories, files, and classpath.

2. New directory buildings is created, new files are created and located in definite directories, and the classpath is set if necessary.

After setup, the tests are run. This consists of running either the javac.exe compiler, the java.exe launcher, or both. That's all there is to it.

A easy Example

To understand what ClasspathTester, does, open the RunAllTests.bat file in your text editor and look at Test1. Each test is a range of sub-tests which are loosely grouped colse to some kind of a central testing theme, and Test1 is one of the simplest.

So what happens in Test1? First it calls banner.bat to add a header to the results file. Then it creates the Hello.java and MyLibClass.java files, by copying them from their corresponding avaj files (the guess for using avaj files is discussed later in this article). Now everything is set up to run some subtests.

Test1A is very simple. agreeing to its description, " The Hello.java file is located in the current directory, and the Hello.class file will be written to the current directory and run from the current directory". As you can imagine, Test1A operates flawlessly.

Test1B and Test1C are designed to show separate ways that using java.exe to inaugurate the Hello.class file can fail. In Test1B the classpath is been set to an empty directory. In Test1C the file Hello.class is erased. Interestingly, these two tests fail in exactly the same way.

Note that all tests results are stored in the results.log file.

How the Tests are Kept Safe

By "safe" I mean safe to your computer, safe to your other data. Since the tests involve batch files that are creating and destroying files and directories, care must be taken to avoid accidentally destroying or writing over other unrelated files. This is accomplished in the cleanup.bat file by avoiding the use of statements like "erase /S *.*" or "rmdir /S *". Instead, all erasures are kept fairly specific.

In fact, the cleanup.bat file consists of the following statements:

set classpath=

rmdir /S /Q aq

rmdir /S /Q EmptyDirectory

erase Hello*.java

erase Hello*.class

erase MyLibClass*.java

erase MyLibClass*.class

This ensures that there will be no tragic accidental loss of unrelated data.

Why Use .avaj Suffix Files

Part of the operation of ClasspathTester involves thoroughly wiping the base directory clean of all .java files, .class files, and subdirectories; therefore, all the suffix of all permanent java files is mangled to avaj. For any test, the suitable avaj files are copied to java files. The ready files are:

Hello.avaj

Hello1.avaj

Hello2.avaj

MyLibClass.avaj

MyLibClass1.avaj

MyLibClass2.avaj

Download Information

You can find and download a zipped copy of the ClasspathTester can be on my description sustain Page

Summary

ClasspathTester is simple, portable, unobtrusive, customizable and solves an annoying improvement problem, the need to quickly test extra classpath situations with javac.exe, java.exe, or both.

Understand The Java Classpath

No comments:

Post a Comment