How To Set Classpath In Linux For Jar Files
Learn how to ready classpath in Java either every bit an environs variable and pass as the command-line argument. During runtime of any Java application, the CLASSPATH is a parameter that tells the JVM where to look for classes and packages.
- The default value of the classpath is "
."(dot), meaning that only the current directory is searched for dependencies. - Specifying either the
CLASSPATHenvironment variable or the-cpcommand line switch overrides this value. - The order in which you specify multiple classpath entries is important. The Java interpreter will look for classes in the directories in the order they announced in the classpath variable.
Java Classpath separators are Bone specific.
Windows –
;[Semicolon]
Linux/Unix –:[Colon]
ane. Setting CLASSPATH as Environment Variable
When you take set the location of jar files that are e'er required during the application runtime, then it's probably the best to add together them in the car's environment variable 'CLASSPATH'.
During awarding runtime, application class loader will always browse the jar files and classes at specified paths in this variable.
To fix CLASSPATH environment variable, find the location of user environment variables in your machine and add together all paths where Jar files are stored. Use the separator betwixt different ii folders, jar files or classes.
You tin notice the user environs variables window by –
- From the desktop, correct click the Estimator icon.
- Choose Properties from the context card.
- Click the Advanced system settings link.
- Click Surroundings Variables. In the section System Variables, detect the
CLASSPATHsurroundings variable and select it. Click Edit. If theCLASSPATHsurroundings variable does not exist, clickNew. - Add all folders separated with separator. Click OK. Shut all remaining windows past clicking OK.
If you lot are creating CLASSPATH for the commencement time, yous need to specify the name for Variable Name in the Windows 10. Use '.' (dot) to denote current directory.
two. Setting CLASSPATH from Command Line
Use -classpath statement to set classpath from command prompt/panel. Use below given commands to set classpath for different requirements.
Let's say we accept a folder named dependency where JAR files and other classes are placed.
2.1. Add together a unmarried jar file in classpath
Beneath syntax examples will add single jar file in classpath.
//WINDOWS $ fix CLASSPATH=.;C:\dependency\framework.jar //Linux/Unix $ consign CLASSPATH=.:/dependency/framework.jar
2.2. Add multiple jar files in classpath
Beneath syntax examples will add together more than 1 jar file in classpath. To do so, simply utilise the delimiter for your operating system (either ; or :) as a separator betwixt the locations specified for the CLASSPATH.
To add all JAR files present in a directory, use wildcard character ('*').
//WINDOWS $ set up CLASSPATH=C:\dependency\framework.jar;C:\location\otherFramework.jar $ ready CLASSPATH=C:\dependency\framework.jar;C:\location\*.jar //Linux/Unix $ export CLASSPATH=/dependency/framework.jar:/location/otherFramework.jar $ export CLASSPATH=/dependency/framework.jar:/location/*.jar
two.iii. Add together multiple classes to classpath
Many times, you may need to add individual classes in classpath besides. To do and so, simply add the folder where classfile is present. e.g. let's say there are 5 .class files are present in location folder which you want to include in classpath.
//WINDOWS $ prepare CLASSPATH=C:\dependency\*;C:\location //Linux/Unix $ consign CLASSPATH=/dependency/*:/location
As a best practice, always organize all JAR files and awarding classes inside i root binder. This may exist the workspace for the application.
Please note that subdirectories contained inside the CLASSPATH would not exist loaded. In order to load files that are contained within subdirectories, those directories and/or files must be explicitly listed in the CLASSPATH.
two.four. Clearing Classpath
If your CLASSPATH environment variable was set to a value that is not right, then you tin can unset CLASSPATH with specifying empty value to it.
set CLASSPATH=
3. Executing programs with '-classpath' or '-cp' choice in Java
Apart from setting classpath to the surround variable, yous tin can pass additional classpath to Java runtime while launching the application using –classpath option or –cp option.
Utilise the . (dot) to include the electric current path into the classpath where the .form file has been generated.
$ javac –classpath C:\dependency\framework.jar MyApp.Java $ coffee –classpath .;C:\dependency\framework.jar MyApp
4. How to Find and Impress CLASSPATH Value
Someday y'all wish to verify all path entries in CLASSPATH variable, you can verify using echo command.
//Windows c:/> echo %CLASSPATH% //Linux/Unix $ echo $CLASSPATH
If CLASSPATH is not set you will get a CLASSPATH: Undefined variable error (Solaris or Linux) console or just %CLASSPATH% printed in windows control prompt.
Happy Learning !!
Read More:
Java – How to set classpath in Windows seven, 8, ten
Java – Set classpath from command line
Source: https://howtodoinjava.com/java/basics/java-classpath/

0 Response to "How To Set Classpath In Linux For Jar Files"
Post a Comment