Example of Subpackage


\begin{lstlisting}
package com.javatpoint.core;
class Simple{
public static v...
...ng args[]){
System.out.println(''Hello subpackage'');
}
}
\end{lstlisting}

To Compile: javac -d . Simple.java
To Run: java com.javatpoint.core.Simple

The output will be Hello subpackage. How to send the class file to another directory or drive? There is a scenario, I want to put the class file of A.java source file in classes folder of c: drive. For example:

Figure 4.8: Subpackages in Java
Image anotherpackage


\begin{lstlisting}
//save as Simple.java
package mypack;
public class Simple{ ...
... args[]){
System.out.println(''Welcome to package'');
}
}
\end{lstlisting}

To Compile:
\begin{lstlisting}
e:\sources> javac -d c:\classes Simple.java
\end{lstlisting}

To Run:

To run this program from e: directory, you need to set classpath of the directory where the class file resides.


\begin{lstlisting}
e:\sources> set classpath=c:\classes;.;
e:\sources> java mypack.Simple
\end{lstlisting}

The -classpath switch can be used with javac and java tool. To run this program from e: directory, you can use -classpath switch of java that tells where to look for class file. For example:


\begin{lstlisting}
e:\sources> java -classpath c:\classes mypack.Simple
\end{lstlisting}

The output will be: Welcome to package.