Basic Usage
Suppose we have a package called org.mypackage containing the classes:
- HelloWorld (main class)
- SupportClass
- UtilClass
and the files defining this package are stored physically under the directory D:\myprogram (on Windows) or /home/user/myprogram (on Linux).
The file structure will look like this:
| Microsoft Windows | Linux |
|---|---|
| D:\myprogram\ | ---> org\ | ---> mypackage\ | ---> HelloWorld.class ---> SupportClass.class ---> UtilClass.class | /home/user/myprogram/ | ---> org/ | ---> mypackage/ | ---> HelloWorld.class ---> SupportClass.class ---> UtilClass.class |
When we invoke Java, we specify the name of the application to run: org.mypackage.HelloWorld. However we must also tell Java where to look for the files and directories defining our package. So to launch the program, we use the following command:
| Microsoft Windows | Linux |
|---|---|
| java -classpath D:\myprogram org.mypackage.HelloWorld | java -classpath /home/user/myprogram org.mypackage.HelloWorld |
where:
- -classpath D:\myprogram sets the path to the packages used in the program (on Linux, -classpath /home/user/myprogram)
- org.mypackage.HelloWorld is the name of the main class
Note that if we ran Java in D:\myprogram\ (on Linux, /home/user/myprogram/) then we would not need to specify the classpath since Java implicitly looks in the current working directory for files containing classes.
Read more about this topic: Classpath (Java), Setting The Path To Execute Java Programs
Famous quotes containing the words basic and/or usage:
“Man has lost the basic skill of the ape, the ability to scratch its back. Which gave it extraordinary independence, and the liberty to associate for reasons other than the need for mutual back-scratching.”
—Jean Baudrillard (b. 1929)
“...Often the accurate answer to a usage question begins, It depends. And what it depends on most often is where you are, who you are, who your listeners or readers are, and what your purpose in speaking or writing is.”
—Kenneth G. Wilson (b. 1923)