6

We are executing standalone java program from shell script, having a comman script to mention classpath, path, etc..In this common script, we have added several classpaths now and number of character is more than 9000. It is working fine in the test env. Will it cause any issue in Production? Any limitation is there in linux to set classpath? What is the max char for command line inputs...

Ganesan MP
  • 387
  • 1
  • 4
  • 10
  • 1
    Err, the test env is there to validate that everything works fine before doing the same thing in production. If you don't trust your test environment, why do you have one? – JB Nizet Jun 28 '12 at 11:21
  • 2
    There's a limitation on how long environment variables can be and there's a limitation on how long the command line can be. You *could* reach both of those with that kind of length (don't know the concrete numbers for any OS by hearth). You might want to start using [classpath wildcards](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html). – Joachim Sauer Jun 28 '12 at 11:22
  • 1
    @JB: In theory the test and production environments should be the same and in theory if the one works on test it will work in production. However, the difference between theory and practise is that in theory, there is no difference between theory and practise. – Jaco Van Niekerk Jun 28 '12 at 11:33
  • rather than a 9000 character classpath on the command-line, just use standard java6 [wildcard ("*") notation](http://docs.oracle.com/javase/6/docs/technotes/tools/solaris/classpath.html) to indicate a directory of jars, e.g., `/path/to/foo.jar:/path/to/many/*:/path/to/classes` (and quote the value to protect it from being expanded by the shell) – michael Apr 18 '13 at 05:26

3 Answers3

5

No, there is no limitation. In Windows there is (8191 characters), but not under Linux. We work with the concept of classpath-files. These file lists all the dependencies for the application, eg:

...
libs/org/easymock/easymock/2.2/easymock-2.2.jar
libs/org/hamcrest/hamcrest-core/1.1/hamcrest-core-1.1.jar
libs/org/hibernate/hibernate-envers/4.1.0.Final/hibernate-envers-4.1.0.Final.jar
libs/com/google/inject/guice/3.0/guice-3.0.jar
...

and then we convert this into usable classpath and run the application as follows:

#!/bin/bash

CLASSPATH_FILE=`ls -r1 ${APP-HOME}/classpaths/myapp*.classpath | head -n1`
CLASSPATH=$(cat $CLASSPATH_FILE | sed 's_^libs_ ${APP-HOME}/libs_' | tr -d '\n' | tr -d '\r' | sed 's_.jar/libs/_.jar:/libs/_g' | sed 's_.pom/libs/_.pom:/libs/_g')

java -d64 -cp $CLASSPATH com.blah.main.Main $@

We have never run into problems and these classpath entries gets pretty huge.

EDIT: As a side note, you can use the maven dependency plugin to generate a list of dependencies.

Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48
  • 5
    I disagree there is no limit in Linux. With an older (2.6.5) kernel we absolutely have hit a hard limit of 128KB for the command-line length. – QuantumMechanic Jun 28 '12 at 11:56
  • 1
    ...Linux never ceases to amaze me. OK, correction then: real operating systems should not give you any problems. :-) (I made the required changes above, thanks QuantumMechanic) – Jaco Van Niekerk Jun 28 '12 at 12:15
0

See this stackoverflow answer about maximum linux command line lengths.

The maximum command line length will be roughly between 128KB and 2MB.

The max size of any one argument is considerably smaller, though, and 9000 chars might be problematic.

Community
  • 1
  • 1
QuantumMechanic
  • 13,795
  • 4
  • 45
  • 66
0

When you use in your Java program some classes from a jar file that is specified in the classpath variable, the JVM won't load that class until your running program will explicitly need that class (or if you load that class explicitly from your code - the same idea). The only problem that can appear when you have a very long classpath, is the time needed for classpath checking before the JVM find the right jar file. But that should not be a problem. If your program behaves well in tests, you should not be worried about this.

artaxerxe
  • 6,281
  • 21
  • 68
  • 106