0

Is it mandatory to add classpath variable in commandline . for e.g.

java -cp $CLASSPATH MyClass

I know it's mandatory if the classpath is stored in some different variable , but since CLASSPATH (the variable) is a parameter in the Java Virtual Machine, does it have to be explicity added to command line ? I am facing a issue where if i don't add the CLASSPATH variable to command line i get NoClassDef.

Kalyan Vedala
  • 1,049
  • 2
  • 17
  • 33
bluelurker
  • 1,353
  • 3
  • 19
  • 27
  • Set it in the System Environment variables – Bhargav Kumar R Feb 25 '16 at 09:05
  • You will face issue if you do not add required jars in classpath as JVM will not know where to find the class. A good explanation of classpath http://stackoverflow.com/questions/2396493/what-is-a-classpath. – sashwat Feb 25 '16 at 09:28

1 Answers1

2

Your CLASSPATH variable probably contains wildcards (*).

When you run java -cp $CLASSPATH, the wildcards are expanded, but not when the class path is read directly from the environment variable. Apart from this issue, CLASSPATH variable is automatically used by Java launcher.

apangin
  • 92,924
  • 10
  • 193
  • 247
  • Thanks . Actually I didn't export the classpath that i was creating in my shell script. Due to which CLASSPATH was not available. – bluelurker Feb 26 '16 at 06:37
  • Do you know if there is some special reason why the wildcard is not expanded if it is read from the environment variable? – Markus Weninger Feb 26 '16 at 07:53
  • 1
    @MarkusWeninger I mean the expansion by a shell, not by a VM. E.g. if I set `CLASSPATH=myapp*.jar` JVM won't recognize `myapp-v1.0.jar`, but if I run `java -cp $CLASSPATH`, `myapp*.jar` will be expanded to `myapp-v1.0.jar` by the shell, and this will work correctly. – apangin Feb 26 '16 at 10:30