0

I have an environment variable called $CLASSPATH which I use as the java classpath to run a certain java code.

This $CLASSPATH variable is quite long. Its size is about 66427 bytes (see how long it is)

Now this is the weird behaviour.

When I append a certain jar file (say bar.jar) to the beginning of $CLASSPATH, the jar file gets added to the classpath and my code runs fine.

I do this in a shell script as follows,

CLASSPATH=/home/foo/bar.jar:$CLASSPATH

But, when I append the bar.jar at the end of the $CLASSPATH, my code gives an exception, due to missing the bar.jar file!

CLASSPATH=$CLASSPATH:/home/foo/bar.jar

Why this weird behaviour?

Am I missing anything here?

Update:

If applicable, please have a look at the output generated from xargs --show-limits command on shell.

foo@foo-laptop:~$ xargs --show-limits
Your environment variables take up 3632 bytes
POSIX upper limit on argument length (this system): 2091472
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2087840
Size of command buffer we are actually using: 131072
Dilini
  • 777
  • 8
  • 22

2 Answers2

1

This question will help you .

It says:

Try with: xargs --show-limits

Your environment variables take up 2446 bytes POSIX upper limit on argument length (this system): 2092658 POSIX smallest allowable upper limit on argument length (all systems): 4096 Maximum length of command we could actually use: 2090212 Size of command buffer we are actually using: 131072 There is no limit per argument, but a total for the whole command line length. In my system (Fedora 15/zsh) its closer to 2Mb. (line 4).

Check the size of your resulting $CLASSPATH

Community
  • 1
  • 1
Aditya Peshave
  • 667
  • 9
  • 26
  • Thanks ADi! Size of my `$CLASSPATH` variable is 66427 bytes. As per the output from `xargs --show-limits` command: `Your environment variables take up 3632 bytes`. Accordingly, if my $CLASSPATH size exceeds this limit of 3632, shouldn't I be getting an error or warning? However, I **do not get such an error/warning**. – Dilini Feb 20 '14 at 11:02
1

When an environment variable like CLASSPATH has a limit on its value length, you can try wild cards on some portions of the path.

As per documentation:

If the directory foo contains a.jar, b.jar, and c.jar, then the class path foo/* is expanded into foo/a.jar;foo/b.jar;foo/c.jar, and that string would be the value of the system property.

When you have so many .class and .jar files in the same path, use the following syntax:

CLASSPATH=/home/foo/*
CLASSPATH=$CLASSPATH:/home/bar/*

Refer to:

Ravinder Reddy
  • 23,692
  • 6
  • 52
  • 82
  • At the moment, I cannot find a way to use your suggestion, as in my case, I pick jar files using a script, one by one, based on certain conditions. However, keeping this 'wildcard' method in mind would help me someday. Thanks Ravinder! – Dilini Feb 20 '14 at 11:41