This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I've written some tools to help me resolve classpath problems on a Windows system. However, I think they are too hard for a beginning programmer to use. Can you give me some ideas for making them easier to use?
The one tool: CheckClasspath is used to test that the classpath reaches a class file. It uses the current environment CLASSPATH value and the package of the class file to see if there is a path to the class. The problem is getting the current CLASSPATH value into the program. I have several ways to start execution of the program. When a program is started with java -jar the classpath is ONLY to the jar.
The one I use the most is from the right-click context menu when I right-click on a class file. The problem here is how can the program get the CLASSPATH value? The only way I've found is to call a batch file which can use the %CLASSPATH% variable to pull in and pass the classpath to the program as a parameter. The batch file has the following commandline: java.exe -jar %JAVA_RUN%\CkClasspath.jar -CP "%CLASSPATH%" %1 %2 %3
Another way is from a shortcut. Here the OS will substitute the classpath for the %CLASSPATH% value in the program parameters on the commandline. The third way is to start the program from the batchfile.
Some people advocate NOT using the CLASSPATH environment variable. The consequences of that is that you have to type in a new classpath every time you create a batch file or shortcut and I make my share of typing errors. The java program does NOT complain if you execute it with a bad/non-existent path. Too bad, that would have saved me hours!!!
An example usage of CheckClasspath -> create a batch file to execute a class file: Create a batch file with the line: java -cp <cp> <path> Open the CheckClasspath program and use it to "Open" the class file. It will display the classpath.path to the file. Select the 'Copy Classpath to CB' menuitem and paste/replace <cp> above. Select the' Copy package to CB' menuitem and paste/replace <path> above.
I suppose some of the IDEs will do all this for you.
What ways have others found to verify correct classpath settings when creating commandlines for executing java programs? java.exe is no help there.
Poor Sun, they're damned if they do, and damned if they don't. This was undeprecated due to public outcry. It's actually not the first deprecated, then undeprecated API, although I can't remember what the other example is at this moment.
Some people advocate NOT using the CLASSPATH environment variable. The consequences of that is that you have to type in a new classpath every time you create a batch file or shortcut and I make my share of typing errors.
You could set a indirect variable ...
... and use it, to create your batch-file:
to avoid retyping a lot of stuff over and over.
Another approach would be to have a template-batch-file, where it is hardcoded inside:
(which is not tested - I got no win by hand). It might be done more elegant if you know how to threat cmd.exe a bit sophisticated, and I guess you got my point.