Creating a Java Class in a Package from the Command Line
Creating a Java Class in a Package from the Command Line
In Java, packages are used to group related classes. Creating a Java class in a package from the command line can be a useful skill to have. To begin, open your command line interface.
$ mkdir mypackage
$ cd mypackage
$ echo ‘package mypackage; public class MyClass {}’ > MyClass.java
Once you have created the MyClass.java file, compile it using the javac command.
$ javac MyClass.java
Your Java class is now compiled. To run it, use the java command with the fully qualified class name.
$ java mypackage.MyClass
Congratulations! You have successfully created and run a Java class in a package from the command line.