Monday, October 15, 2007

Solution to classpath too long (aka input line too long) problem in Windows

If you use Java in Windows, you're bound to run into classpath too long problem when your classpath grows. Windows has a limit (1KB - 2KB) of characters you can have on one single command line. The infamous "Input line is too long" is very annoying. Here's a trick to get around it.

"java.exe" command also scan for classes from the environment variable "CLASSPATH". If you can break your classpath into separate folders and jars, you can concatenate them like this:


setlocal

set CLASSPATH=c:/my/first/jar/somejar.jar;%CLASSPATH%
set CLASSPATH=c:/my/second/jar/someotherjar.jar;%CLASSPATH%
set CLASSPATH=c:/path/to/a/folder;%CLASSPATH%
.......
.......

java com.mycompany.Main

endlocal

By using CLASSPATH env variable, you don't need to pass it in to "java" command. The setlocal/endlocal pair ensures the CLASSPATH is "local" to this process and won't pollute the systemwise value.

--------
Updated:

Travis has pointed out that there is a limit on environment variable in Windows. I found it limited to 8K. So this isn't an absolute solution but it should sustain you for awhile. As suggested by D, you could shorten the path by using virtual drive. Further more, JDK6 has supported wildcard (*) in classpath.

Virtual drive:
Say you have bunch of jars under c:/path/to/lib

subst z: c:/my/path/to/lib
set CLASSPATH=z:/jar1.jar:%CLASSPATH%

Or with JDK6:

set CLASSPATH=c:/path/to/lib/*:%CLASSPATH%

17 comments:

Michael Easter said...

Absolutely... It can be really liberating to start with an empty CLASSPATH var and then start adding to it.

If you are interested, here is a script that uses Groovy to 'glob' jars recursively and build up the CLASSPATH env var

D said...

Another thing you can do is create a virtual drive using 'subst' and then put everything in the root of that drive. Another option is to put the classpath in the manifest file of your jar.

Orion Letizi said...

Windows rules. I love how easy and obvious it is to get things done.

Travis said...

Actually, this is not an adequate solution in all cases. There is actually a limit to the size of what an environment variable can hold. I can't remember the specifics, but I've ran into problems using the environment variable approach as well. A solution I found was to use something like ClassWorld's loader utility.

Hung Huynh said...

Travis, you are right. The max size for env variable is 8KB. This solution along with the suggested virtual drive by D, could sustain you for awhile. The classpath in manifest file seems to solve it more elegantly if you have control of your production jar. I haven't used ClassWorlds, though it seems more involved than just a simple classpath.

Евгений said...

Like Linus said, "If you need more than 3 levels of indentation, you're screwed anyway..." :-) .

Charlie "Atrox" Collins said...

I am not a Windows guy, but someone on the GWT-Maven forum mentioned: "I was able to extend the comspec on windows by adding /e:32768. As a
result of this change I am no longer getting the 'line too long'
error."

Would that work here, does it make sense?

admir said...

I solved this problem by checking if OS is WINDOWS and then calling a .cmd script from my java program.
The script is created at runtime and it does not use env variables which themselves have a size limition of 8KB.

Geoffrey said...

Just use line continuation.

Jonny Bruno said...

Have you ever tried to copy or delete files on your computer and been told that “The filename you specified is invalid or too long. Specify a different filename”?
If you have, you know that you can’t perform a backup of your files, as Windows will cancel the process at the point of the problem, leaving you wondering what was copied, and what was not.
The problem is caused by Windows (98, 2000, XP & Vista) not allowing file paths of more that 231 characters. It's a time consuming problem for us to fix, if indeed, we can fix it at all.

Long Path Tool solves this problem and allows files with long paths to be copied and deleted.

Alex Hugy said...

path too long fixer

Silver MLM said...

If there is a problem in windows you can run in safe mode. But how to get SAFE MODE in Linux?

john said...

Long Path Tool helped me to solve that problem
http://LongPathTool.com

Markgaydes said...

If you don;t mind i have a suggestion

you can try  to solve it

thanks
this tool

long path tool
 

shahin kabir said...

you can try this
path too long tools
 

Jokmokk said...

Here is a nice solution for the same problem: http://kindofjava.blogspot.de/2011/06/java-classpath-via-maven-too-long.html

GarryBrown said...

Thanks for the information. Also try Long Path Tool. It helped me with Error 1320 in Win 7. :)