0% found this document useful (0 votes)
13 views3 pages

Classpath

The document discusses setting the classpath for Java programs when classes are located in different directories or packages. It explains how to compile and run programs in these cases. It also covers creating JAR files, using JARs in the classpath, and making executable JAR files.

Uploaded by

bzcodertraining
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Classpath

The document discusses setting the classpath for Java programs when classes are located in different directories or packages. It explains how to compile and run programs in these cases. It also covers creating JAR files, using JARs in the classpath, and making executable JAR files.

Uploaded by

bzcodertraining
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Setting classpath for current execution for class files

Compiling a java file having reference to a class present in different directory but default package
- Main.java present in C:\BZ_Coder_Training\Java_programs
- Contains a reference to Employee.class present in C:\BZ_Coder_Training\test
To compile Main.java
javac -cp C:\BZ_Coder_Training\test Main.java
To run Main.class
- To run Main class also we need to provide the classpath using -cp but when we use -cp flag java will
only look for classes in that classpath even for Main.class. If we use
java -cp C:\BZ_Coder_Training\test Main
then java will look for Main.class in C:\BZ_Coder_Training\test instead of looking into cwd, so we need to
provide two classpaths which will be separated by ;
java -cp C:\BZ_Coder_Training\test;C:\BZ_Coder_Training\Java_programs Main

Compiling a java file having reference to a class present in different directory but in some package
- Main.java present in C:\BZ_Coder_Training\Java_programs
- Contains a reference to Employee.class present in C:\BZ_Coder_Training\test in com.bzcoder.test
To compile Main.java
use import statement in Main.java as import com.bzcoder.test.Employee
then compile like below
javac -cp C:\BZ_Coder_Training\test Main.java
To run Main.class
- To run Main class also we need to provide the classpath using -cp but when we use -cp flag java will
only look for classes in that classpath even for Main.class. If we use
java -cp C:\BZ_Coder_Training\test Main

then java will look for Main.class in C:\BZ_Coder_Training\test instead of looking into cwd, so we need to
provide two classpaths which will be separated by ;
java -cp C:\BZ_Coder_Training\test;C:\BZ_Coder_Training\Java_programs Main

Setting classpath permanently for class files


In environment variables add a variable in system variables:
Variable name: CLASSPATH
Variable value: ; separated paths to class files
Example: C:\BZ_Coder_Training\test;C:\BZ_Coder_Training\Java_programs  as variable value

BZ Coder Training Center Mobile: 6299174461


Author: Aditya Singhania Email: [email protected]
1
Creating a jar file and using it in classpath
Note: All the class files or other files or packages to be included in jar must be present in
current working directory.

1. To create a jar file use following command


To include specific class files
Syntax:
jar -cvf jar_name.jar space_separated_class_files_to_include
Example:
jar -cvf myapp.jar Main.class Employee.class
To include all class files
Syntax:
jar -cvf jar_name.jar *.class
Example:
jar -cvf *.class  includes all the class files in CWD
To include class files and other files
Syntax:
jar -cvf jar_name.jar space_separated_class_files_to_include other_files
Example:
jar -cvf myapp.jar Main.class Employee.class Hello.txt
jar -cvf myapp.jar *.class Hello.txt
To include class files and packages
Syntax:
jar -cvf jar_name.jar space_separated_class_files_to_include other_files
Example:
jar -cvf myapp.jar Main.class Employee.class com/*  my class files are in package com.bzcoder.test

Using jars as classpath for current execution


To compile:
javac -cp C:\BZ_Coder_Training\ Java_programs\myapp.jar Main.java
To run:
Java -cp C:\BZ_Coder_Training\ Java_programs\myapp.jar; C:\BZ_Coder_Training\ Java_programs\ Main

Using jars as classpath for current execution


In environment variables add a variable in system variables:
Variable name: CLASSPATH
Variable value: ; separated paths to class files or jars
Example: C:\BZ_Coder_Training\test;C:\BZ_Coder_Training\Java_programs\myapp.jar  as variable value

BZ Coder Training Center Mobile: 6299174461


Author: Aditya Singhania Email: [email protected]
2
Creating an executable jar file
1. Create MANIFEST.MF file with following information.
Manifest-Version: 1.0
Main-Class: MainClassName (replace with your actual class name)
2. Package the required files.
jar -cvf <jar_file_name.jar> MANIFEST.MF <files_to_archive>
Example:
jar -cvf myapp.jar MANIFEST.MF Main.class Employee.class

To run a jar file use following command:


java -jar myapp.jar

BZ Coder Training Center Mobile: 6299174461


Author: Aditya Singhania Email: [email protected]
3

You might also like