Fix "The input line is too long" error in Kafka CLI tools on Windows

My name is Alexander. I am currently a back-end Java developer for Fintech companies and sometimes I write about tech-related topics.
Say, you have a Kafka cluster running somewhere (may be in Docker containers or remote servers) and you need to connect to Kafka from your Windows development machine. Kafka ships bundled with their own set of CLI tools for managing Kafka clusters. Unfortunately, the Kafka CLI Tools do not run smooth on MS Windows. The other day I was trying to use the tools in Kafka version 3.9.1 and run into errors. I’ll how you how I fixed it.
The problem
You want to install and use Kafka CLI tools on MS Windows but you get the error “The input line is too long” when running commands. For example, attempt to run command .\kafka-topics.bat —help
and it fails with error “The input line is too long” as shown on the picture below.
The solution guide
Skip the next two steps if you just wont the solution and do not need any demo.
Download and install Kafka CLI Tools
Go to Apache Kafka downloads page and download the appropriate binary distribution for Windows. Then extract the files to a local directory. Since Kafka binary distributions are compressed into a .tgz
file format (.tar
inside a .tgz
archive), you may need to extract it twice - first to a .tar
file and then to the target destination. Now you’ll see all the CLI tools as batch scripts in the directory kafka-destination-folder\bin\windows
. In my case it is C:\Programs\kafka_2.13-3.9.1\bin\windows
.
We are now ready to try and test Kafka CLI tools. Now if you run command .\kafka-topics.bat —help
and it works fine, you don’t need any fixes. But if it fails with error “The input line is too long” you can follow the next steps and fix it.
The reason is the maximum length of the string that you can use at the Windows command prompt is 8191 characters. Most Kafka CLI tools execute the core script kafka-run-class.bat
to run Java programs. Now this script builds a very long command string that violates the said length 8191 characters limit.
Let’s take a look at C:\Programs\kafka_2.13-3.9.1\bin\windows\kafka-run-class.bat
. The script has the code starting at line 179 looking like this:
set COMMAND=%JAVA% %KAFKA_HEAP_OPTS% %KAFKA_JVM_PERFORMANCE_OPTS% %KAFKA_JMX_OPTS% %KAFKA_LOG4J_OPTS% -cp "%CLASSPATH%" %KAFKA_OPTS% %*
rem echo.
rem echo %COMMAND%
rem echo.
%COMMAND%
It is not very easy to find the exact line that causes the error: I used echo-outputs unless I understood that the command line was too long because of big Java class path line. It is a common cause for such errors.
The fix is simple - we’ll use the argument files feature of the Java command. It helps to pack long strings of command line arguments away into files and then reference those files in a java command via @-sign notation (look it up in Java docs).
First, I piped the CLASSPATH variable contents into a text file classpath.txt in the user’s TEMP directory like this echo "%CLASSPATH%" > %TEMP%\classpath.txt
. And then I referenced the file as argument file in the final COMMAND variable like this -cp @%TEMP%\classpath.txt
. Well, the fixed lines of code look like this:
rem Save ClassPath to an argsFile
echo "%CLASSPATH%" > %TEMP%\classpath.txt
set COMMAND=%JAVA% %KAFKA_HEAP_OPTS% %KAFKA_JVM_PERFORMANCE_OPTS% %KAFKA_JMX_OPTS% %KAFKA_LOG4J_OPTS% -cp @%TEMP%\classpath.txt %KAFKA_OPTS% %*
rem echo.
rem echo %COMMAND%
rem echo.
%COMMAND%
Finally, when we run the commands they work. Commands .\kafka-topics.bat —version and .\kafka-topics.bat —help give us the normal output.
I like it! Congratulations and good luck!
Subscribe to my newsletter
Read articles from Alexander Kuznetsov directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
