亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Table of Contents
Using the -cp or -classpath Option
Setting the CLASSPATH Environment Variable
Including Multiple JAR Files
Using a Manifest File in a Runnable JAR
Home Java javaTutorial How to add a JAR file to the classpath in Java?

How to add a JAR file to the classpath in Java?

Sep 21, 2025 am 05:09 AM
java

Use the -cp parameter to add the JAR to the classpath, so that the JVM can load its internal classes and resources, such as java -cp library.jar com.example.Main, which supports multiple JARs separated by semicolons or colons, and can also be configured through CLASSPATH environment variables or MANIFEST.MF.

How to add a JAR file to the classpath in Java?

To include a JAR file in the classpath when running a Java application, you need to make the classes and resources inside that JAR available for loading at runtime. There are several ways to do this depending on how you're launching your program.

Using the -cp or -classpath Option

The most common way is to use the -cp (or -classpath ) flag with the java command. This tells the JVM where to look for user-defined classes and packages, including those in JAR files.

  • If your main class is com.example.Main and the JAR is named library.jar , run:
  • java -cp library.jar com.example.Main

  • You can also include the current directory ( . ) along with the JAR if your own compiled classes are in the local folder:
  • java -cp ".;library.jar" com.example.Main (on Windows)

    java -cp ".:library.jar" com.example.Main (on Linux/macOS)

Setting the CLASSPATH Environment Variable

You can set the CLASSPATH environment variable instead of using -cp every time.

  • On Linux/macOS:
  • export CLASSPATH=.:library.jar

  • On Windows:
  • set CLASSPATH=.;library.jar

  • After setting it, you can run your program without specifying -cp :
  • java com.example.Main

  • Note: Relying on environment variables can lead to issues in different environments, so using -cp is often preferred.

Including Multiple JAR Files

If your application depends on more than one JAR, you can list them all in the classpath.

  • On Linux/macOS, separate paths with colons ( : ):
  • java -cp "library.jar:utils.jar:." com.example.Main

  • On Windows, use semicolons ( ; ):
  • java -cp "library.jar;utils.jar;." com.example.Main

  • As a shortcut, you can use * to include all JARs in a directory (but not subdirectories):
  • java -cp "lib/*" com.example.Main

Using a Manifest File in a Runnable JAR

If you're building a runnable JAR, you can specify the classpath in the META-INF/MANIFEST.MF file.

  • Add a line like:
  • Class-Path: library.jar utils.jar

  • This assumes the listed JARs are in the same directory as your main JAR.
  • Then run with:
  • java -jar myapp.jar

Basically, adding a JAR to the classpath ensures the JVM can locate and load its classes. The -cp option is reliable and widely used. Just be careful with path separators and relative paths. It's not complex but easy to get wrong if syntax isn't correct.

The above is the detailed content of How to add a JAR file to the classpath in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

ArtGPT

ArtGPT

AI image generator for creative art from text prompts.

Stock Market GPT

Stock Market GPT

AI powered investment research for smarter decisions

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

How to use the Optional class to avoid NullPointerException in Java? How to use the Optional class to avoid NullPointerException in Java? Sep 25, 2025 am 06:04 AM

The Optional class is used to safely handle values ??that may be null, avoiding null pointer exceptions. 1. Create an instance using Optional.ofNullable to handle null values. 2. Check and access values ??through isPresent or ifPresent security to avoid direct call to get to cause exceptions. 3. Use orElse and orElseGet to provide default values, or use orElseThrow to throw a custom exception. 4. Convert or filter values ??through map and filter chain operations to improve code readability and robustness.

How to get the class of an object in Java? How to get the class of an object in Java? Sep 26, 2025 am 04:58 AM

Use the getClass() method to get the runtime class of the object, such as str.getClass() to return the Class object; for types, you can directly use the String.class syntax. The Class class provides methods such as getName(), getSimpleName() to obtain class information, such as num.getClass().getSimpleName() to output Integer.

How to create a multi-dimensional array in Java? How to create a multi-dimensional array in Java? Sep 25, 2025 am 05:37 AM

Atwo-dimensionalarrayinJavaisanarrayofarrays,declaredwithtwobrackets,likeint[][]matrix,andcanbeinitializedwithvaluesorusingnew;forexample,int[][]matrix={{1,2},{3,4}};createsa3x2matrix.

How to get the current working directory in Java? How to get the current working directory in Java? Sep 26, 2025 am 05:51 AM

ThecurrentworkingdirectoryinJavacanbeobtainedusingSystem.getProperty("user.dir"),whichreturnstheabsolutepathwheretheprogramwaslaunched;alternatively,Paths.get("").toAbsolutePath().toString()fromtheNIOAPIprovidesthesameresult,witht

What is a Singleton class in Java? What is a Singleton class in Java? Sep 25, 2025 am 05:30 AM

AsingletonclassinJavaensuresonlyoneinstanceexiststhroughoutanapplication’slifecyclebyusingaprivateconstructor,aprivatestaticinstance,andapublicstaticgetInstance()method;commonimplementationsincludeeagerinitialization,lazyinitialization,thread-safelaz

What is the concept of Generics in Java? What is the concept of Generics in Java? Sep 26, 2025 am 05:19 AM

GenericsinJavaprovidecompile-timetypesafetyandeliminatetheneedforcastingbyallowingclasses,interfaces,andmethodstooperateontypeparameters;forexample,usingListensuresonlystringscanbeadded,preventingruntimeClassCastExceptions;genericsworkviatypeparamete

How to clear cache and cookies of a single website UC browser. UC browser targeted website cache cookies skills How to clear cache and cookies of a single website UC browser. UC browser targeted website cache cookies skills Sep 26, 2025 pm 12:33 PM

Caches and cookies can be cleaned for specific websites to resolve UC browser page loading exceptions. 1. Go to Settings → Privacy and Security → Website Data Management, search for the target website and clear its data; 2. Use the invisible browsing mode to access the problem website to avoid data retention; 3. Reset the storage by disabling and enabling website permissions, and force clear the old cache.

How to export history records by Wukong Browser_Wukong Browser Browser History Export and Backup Method How to export history records by Wukong Browser_Wukong Browser Browser History Export and Backup Method Sep 26, 2025 pm 12:54 PM

You can back up Wukong browser history by manually recording, extracting databases or automated scripts. First, you can enter the history page to copy or take photos and save them manually; secondly, if the device is rooted, you can access the /data/data/com.wukong.browser/databases/ path with the file manager, export the history.db database and parse it into CSV with the SQLite tool; finally, for rootless devices, you can use Auto.js and other tools to write scripts, call the accessibility service to automatically slide the historical page and take screenshots to archive, and realize semi-automated backup.

See all articles