Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA_TOOL_OPTIONS maximum length

Tags:

java

jar

I have a simple jar with Hello.java:

class Hello {
   public static void main(String[] a) {
      System.out.println("Hello world!");
   }
}

I'd like to pass it some options via JAVA_TOOL_OPTIONS. If value of $JAVA_TOOL_OPTIONS length is less or equal to 1024, it is picked up:

$export JAVA_TOOL_OPTIONS=$(for i in {1..43}; do echo -n "-Dmyapp.opt${i}="123456789" "; done)
$ echo $JAVA_TOOL_OPTIONS | wc -c
1023

java -jar hello.jar
Picked up JAVA_TOOL_OPTIONS: -Dmyapp.opt1=123456789 -Dmyapp.opt2=123456789 -Dmyapp.opt3=123456789 -Dmyapp.opt4=123456789 -Dmyapp.opt5=123456789 -Dmyapp.opt6=123456789 -Dmyapp.opt7=123456789 -Dmyapp.opt8=123456789 -Dmyapp.opt9=123456789 -Dmyapp.opt10=123456789 -Dmyapp.opt11=123456789 -Dmyapp.opt12=123456789 -Dmyapp.opt13=123456789 -Dmyapp.opt14=123456789 -Dmyapp.opt15=123456789 -Dmyapp.opt16=123456789 -Dmyapp.opt17=123456789 -Dmyapp.opt18=123456789 -Dmyapp.opt19=123456789 -Dmyapp.opt20=123456789 -Dmyapp.opt21=123456789 -Dmyapp.opt22=123456789 -Dmyapp.opt23=123456789 -Dmyapp.opt24=123456789 -Dmyapp.opt25=123456789 -Dmyapp.opt26=123456789 -Dmyapp.opt27=123456789 -Dmyapp.opt28=123456789 -Dmyapp.opt29=123456789 -Dmyapp.opt30=123456789 -Dmyapp.opt31=123456789 -Dmyapp.opt32=123456789 -Dmyapp.opt33=123456789 -Dmyapp.opt34=123456789 -Dmyapp.opt35=123456789 -Dmyapp.opt36=123456789 -Dmyapp.opt37=123456789 -Dmyapp.opt38=123456789 -Dmyapp.opt39=123456789 -Dmyapp.opt40=123456789 -Dmyapp.opt41=123456789 -Dmyapp.opt42=123456789 -Dmyapp.opt43=123456789
Hello world!

If value of $JAVA_TOOL_OPTIONS length is more than 1024, it's ignored:

$export JAVA_TOOL_OPTIONS+="$JAVA_TOOL_OPTIONS -Dmyapp.opt44=123456789"
$ echo $JAVA_TOOL_OPTIONS | wc -c
2070

$ java -jar hello.jar
Hello world!

Where from does JAVA_TOOL_OPTIONS characters length limit comes from? Is it possible to exceed 1024 char limit?

like image 563
idobr Avatar asked Feb 06 '23 06:02

idobr


1 Answers

That's an interesting question! If you check the source code of the OpenJDK v7, in particular the command line argument parser, you'll see:

jint Arguments::parse_options_environment_variable(const char* name, SysClassPath* scp_p, bool* scp_assembly_required_p) {
  const int N_MAX_OPTIONS = 64;
  const int OPTION_BUFFER_SIZE = 1024;
  char buffer[OPTION_BUFFER_SIZE];

So the buffer size is limited to 1024, which exactly matches what your nice experiments confirmed :-)

However, I don't know of and did not find any official documentation as to why this limit exists.

like image 59
dnswlt Avatar answered Feb 08 '23 16:02

dnswlt