July 20, 2009

Javac Command Line Argument File

Recently I ran into a problem where I have too many jars in my classpath to successfully compile a certain application. Does this indicate something is broken in the build system? Definitely. But, until the build system is fixed I still need to compile, right?

There are stupid things to do with the path to get this working, but it looks like the engineers at Sun (when it was Sun) had (sort of) thought this might be a problem in the future, so they build something into the Java compiler called Command Line Argument Files. Basically you can specify all of your javac arguments within a single (or multiple) file(s) and not be limited by the underlying OS or shell program. This is awesome, except it's sort of broken when using classpath entries with spaces.

Luckily, there are workarounds.

First, the problem. When you have a classpath entry with a space, like the following

-classpath C:\Program Files\Java\jdk1.5.0_16\jre\lib\plugin.jar

the compile gives you this error

javac: invalid flag: Files\Java\jdk1.5.0_16\jre\lib\plugin.jar

My first attempt was to put the entry in quotes - didn't work. Then I tried to use forward slashes (/) - didn't work.

The only solution I could come up with was to replace the tokens with spaces to their 8.3 names, thus rendering the line to this

-classpath C:\PROGRA~1\Java\jdk1.5.0_16\jre\lib\plugin.jar

Since putting spaced classpath entries inside quotes works on the command line I'm assuming this is a bug with the way the argument file is parsed. But at least you have a fix.

Posted by Guy at July 20, 2009 12:01 PM