Both Windows and Unix have the concept of a Path variable. As an example open up dos command prompt on your Windows system and do:
C:\Users\aj1>echo %PATH%
C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\TXE Components\TCS\;C:\Program Files\Intel\TXE Components\TCS\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Users\aj1\AppData\Local\Microsoft\WindowsApps;
Now type in "notepad" and the Windows Notepad application starts up.
C:\Users\aj1>notepad
C:\Users\aj1>
What Windows did was it went through each folder in the path and checked for "notepad.exe" and it found it in one of the paths and started it up. The folder that it found the "notepad.exe" was in "C:\windows\system32" . Let's try changing the PATH variable.
C:\Java>set PATH=""
C:\Java>notepad
'notepad' is not recognized as an internal or external command,
operable program or batch file.
Now Windows cannot find the "notepad" executable because our PATH environment is empty and there are no folders available.
If "javac" cannot be found then we need to place the JDK folder in our PATH also. We can do that with the command:
set J2SDK_PATH=C:\jdk18
set PATH=%PATH%;%J2SDK_PATH%\bin;
The "J2SDK_PATH" is our custom variable and we are using that in the second line to add the "C:\jdk18\bin" to our existing path. We can place the above 2 lines in a batch file called "set1.bat" and then run this batch file whenever we are in a Dos session.
Java relies on an environment variable called "CLASSPATH" for it's execution of Java programs. This is not a variable that is used by Windows but created for Java programs. Java uses it in a similar manned to the PATH variable used by Windows.
Let's say we have a sample Java program in the folder :
C:\Java\packages>
We shall call our file "default1.java".
File: "default1.java"
import java.util.* ;
public class default1
{
public static void main(String args[] )
{
System.out.println("Inside default1" ) ;
}
}
Now from the same folder we can compile and run our program.
C:\Java\packages>javac default1.java
C:\Java\packages>java default1
Inside default1
Now let us change folders to some other folder say "C:\Java\packages_test>" .
C:\Java\packages_test>java default1
Error: Could not find or load main class default1
Ok now the Java could not find the class file. In the previous method the class file was in the current folder and that's why Java could find it. We need some way of telling Java where to look for the class file and we make use of the "CLASSPATH" variable to do this.
C:\Java\packages_test>set CLASSPATH=C:\java\packages
C:\Java\packages_test>java default1
Inside default1
C:\Java\packages_test>
Now Java finds our class. It does the same thing that Windows did for the PATH variable. It looks in the folders in the CLASSPATH variable searching for our class.
We have been using expressions like "import java.util.*" in our code. Let's say we have 100 Java classes. How do we organize them ? We could place them in one folder but that's very messy. Instead Java lets us organize these using folders and sub folders.
C:\Java\packages>mkdir com
C:\Java\packages>cd com
C:\Java\packages\com>mkdir ajay
C:\Java\packages\com>cd ajay
C:\Java\packages\com\ajay>dir
Volume in drive C is TI10715900G
Volume Serial Number is B8C9-80A9
Directory of C:\Java\packages\com\ajay
05/23/2019 05:46 AM <DIR> .
05/23/2019 05:46 AM <DIR> ..
05/23/2019 05:48 AM 285 mymath1.java
In the above we have created the folders com and ajay under "C:\Java\packages" .
File: "mymath1.java"
package com.ajay ;
import java.util.* ;
public class mymath1
{
public static int sum( int x1, int x2 )
{
return ( x1 + x2 ) ;
}
public static void main(String args[] )
{
System.out.println("Inside default1" ) ;
}
}
We have the statement "package com.java" in the above program. What this is saying is that this file should be placed in the folder:
"com\ajay"
Now we are going to go to the ajay folder and compile the file.
C:\Java\packages\com\ajay>javac mymath1.java
And that works well. Now lets try to run the program.
C:\Java\packages\com\ajay>java mymath1
Error: Could not find or load main class mymath1
C:\Java\packages\com\ajay>java com.ajay.mymath1
Error: Could not find or load main class com.ajay.mymath1
C:\Java\packages>java mymath1
Error: Could not find or load main class mymath1
C:\Java\packages\com\ajay>cd ../..
C:\Java\packages>java com.ajay.mymath1
Inside default1
Notice the single successful case. We are in the "packages" folder and then we give the command "java com.ajay.mymath1" . If we have a class file in a package then we must run it using the full name "com.ajay.mymath1" . What Java will do is look in the current folder for a folder called "com" and then a subfolder inside it called "ajay" and then look for the class "mymath1" . What if want to run this from another location ?
C:\Java\packages_test>set CLASSPATH=""
C:\Java\packages_test>java com.ajay.mymath1
Error: Could not find or load main class com.ajay.mymath1
C:\Java\packages_test>
As you might have guessed by now we have to tell Java where our "com.ajay" folders are and we do that by using the CLASSPATH .
C:\Java\packages_test>set CLASSPATH=C:\Java\packages
C:\Java\packages_test>java com.ajay.mymath1
Inside default1
Now Java will look through each folder in the CLASSPATH and look for the folder "com/ajay" inside each folder. Now we have a better scheme than placing all the files in a single folder and we can run our class files from another location but we can still do better. This is where the Jar files come in.
A Jar file is a "zip" file. In fact we can change the extension of a ".jar" file to a "zip" file and open the zip file with our Windows Explorer . A Jar file is a collection of classes along with some metadata. Recall that a file in a zip file also can have the folder information along with the file info. We can have zip files that we unzip and the file is unzipped to a folder and sub folders. What we are going to do is take our program "mymath1.java" and place the class file in a Jar file. It is easiest to make a Jar file with an IDE. We will use Netbeans but we can use ay IDE .
Most of them will have the feature to create Jar files from a Java project. Below are 2 snapshots showing the configuration .
For netbeans we shall do a build and that automatically creates a Jar file in the "dist" folder in our project folder. Now to run our program from say:
C:\Java\packages_test>
We are going to place the path to the Jar file in the classpath so that Java knows where it is.
C:\Java\packages_test>set CLASSPATH=C:\NetbeanProjects\Jar1\dist\Jar1.jar
C:\Java\packages_test>java com.ajay.mymath1
Inside default1
C:\Java\packages_test>
We can package our classes in a Jar file and then give that single Jar file to the client who can then run it with the command:
C:\Java\packages_test>java -jar C:\NetbeanProjects\Jar1\dist\Jar1.jar
Inside default1
In fact if we study the files that come with the JDK/JRE distribution, many of them are also Jar files.
Ex 1 )