
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to load classes at runtime from a folder or Java package
Using CLASSPATH, you can load any classes at runtime.
Like the .java source files, the compiled .class files should be in a series of directories that reflect the package name. However, the path to the .class files does not have to be the same as the path to the .java source files. You can arrange your source and class directories separately, as −
<path-one>\sources\com\apple\computers\Dell.java <path-two>\classes\com\apple\computers\Dell.class
By doing this, it is possible to give access to the classes directory to other programmers without revealing your sources. You also need to manage source and class files in this manner so that the compiler and the Java Virtual Machine (JVM) can find all the types your program uses.
The full path to the classes directory, <path-two>\classes, is called the classpath and is set with the CLASSPATH system variable. Both the compiler and the JVM construct the path to your .class files by adding the package name to the classpath.
Say <path-two>\classes is the class path, and the package name is com.apple.computers, then the compiler and JVM will look for .class files in <path-two>\classes\com\apple\computers.
A classpath may include several paths. Multiple paths should be separated by a semicolon (Windows) or colon (Unix). By default, the compiler and the JVM search the current directory and the JAR file containing the Java platform classes so that these directories are automatically in the classpath.
- Related Articles
- Can I import same package twice? Will JVM load the package twice at runtime?
- How to import classes from within another directory/package in Java?
- How to resolve "Could not find or load main class package" in Java?
- How to put two public classes in a Java package.
- How to use classes in other package in Java
- How to access Java package from another package
- How to play videos in Android from assets folder or raw folder?
- Which function should be used to load a package in R, require or library?
- How to get the directories (only) from a folder using Java?
- How to remove a function at runtime in PHP?
- How to skip TestNG test at runtime?
- How to get list of all files/folders from a folder in Java?
- How to change a textView Style at runtime in android?
- Dynamic method dispatch or Runtime polymorphism in Java
- How can we define a Python function at runtime?
