adding classpath in linux

后端 未结 5 1818
梦毁少年i
梦毁少年i 2020-12-03 02:11
export CLASSPATH=.;../somejar.jar;../mysql-connector-java-5.1.6-bin.jar
java -Xmx500m folder.subfolder../dit1/some.xml
cd ..

is the above statement

相关标签:
5条回答
  • For linux users, and to sum up and add to what others have said here, you should know the following:

    1. Global variables are not evil. $CLASSPATH is specifically what Java uses to look through multiple directories to find all the different classes it needs for your script (unless you explicitly tell it otherwise with the -cp override).

    2. The colon (":") character separates the different directories. There is only one $CLASSPATH and it has all the directories in it. So, when you run "export CLASSPATH=...." you want to include the current value "$CLASSPATH" in order to append to it. For example:

      export CLASSPATH=.
      export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar
      

      In the first line above, you start CLASSPATH out with just a simple 'dot' which is the path to your current working directory. With that, whenever you run java it will look in the current working directory (the one you're in) for classes. In the second line above, $CLASSPATH grabs the value that you previously entered (.) and appends the path to a mysql dirver. Now, java will look for the driver AND for your classes.

    3. echo $CLASSPATH
      

      is super handy, and what it returns should read like a colon-separated list of all the directories you want java looking in for what it needs to run your script.

    4. Tomcat does not use CLASSPATH. Read what to do about that here: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html

    0 讨论(0)
  • 2020-12-03 02:37

    Important difference between setting Classpath in Windows and Linux is path separator which is ";" (semi-colon) in Windows and ":" (colon) in Linux. Also %PATH% is used to represent value of existing path variable in Windows while ${PATH} is used for same purpose in Linux (in the bash shell). Here is the way to setup classpath in Linux:

    export CLASSPATH=${CLASSPATH}:/new/path
    

    but as such Classpath is very tricky and you may wonder why your program is not working even after setting correct Classpath. Things to note:

    1. -cp options overrides CLASSPATH environment variable.
    2. Classpath defined in Manifest file overrides both -cp and CLASSPATH envorinment variable.

    Reference: How Classpath works in Java.

    0 讨论(0)
  • Paths under linux are separated by colons (:), not semi-colons (;), as theatrus correctly used it in his example. I believe Java respects this convention.

    Edit

    Alternatively to what andy suggested, you may use the following form (which sets CLASSPATH for the duration of the command):

    CLASSPATH=".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar" java -Xmx500m ...
    

    whichever is more convenient to you.

    0 讨论(0)
  • 2020-12-03 02:43

    It's always advised to never destructively destroy an existing classpath unless you have a good reason.

    The following line preserves the existing classpath and adds onto it.

    export CLASSPATH="$CLASSPATH:foo.jar:../bar.jar"
    
    0 讨论(0)
  • 2020-12-03 02:44

    I don't like setting CLASSPATH. CLASSPATH is a global variable and as such it is evil:

    • If you modify it in one script, suddenly some java programs will stop working.
    • If you put there the libraries for all the things which you run, and it gets cluttered.
    • You get conflicts if two different applications use different versions of the same library.
    • There is no performance gain as libraries in the CLASSPATH are not shared - just their name is shared.
    • If you put the dot (.) or any other relative path in the CLASSPATH that means a different thing in each place - that will cause confusion, for sure.

    Therefore the preferred way is to set the classpath per each run of the jvm, for example:

    java -Xmx500m -cp ".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar"    "folder.subfolder../dit1/some.xml
    

    If it gets long the standard procedure is to wrap it in a bash or batch script to save typing.

    0 讨论(0)
提交回复
热议问题