Creating a New Package Flashcards
What is a default package ?
The default package is a special unnamed package that you should use only for throwaway code. You can tell the code is in the default package, because there’s no package name
How is The directory structure on your computer is related to the package name ?
- Package Naming Convention :
A package name typically follows a hierarchical structure using dots (.) to separate different levels. For example, the package name com.example.project indicates a hierarchy.
- Directory Structure :
Each component of the package name corresponds to a directory on your file system. For com.example.project, the directory structure would be :
EXAMPLE :
com/
example/
project/
ClassName.java
- Compilation:
When you compile Java files, the Java compiler looks for the classes in the corresponding directory structure that matches the package name. This ensures that classes are organized correctly.
- Access :
The directory structure must match the package declaration in the Java file. If the package name does not align with the directory structure, the compiler will throw an error.
can you compile 2 files at the same time using the javac command ?
You can compile multiple Java files at the same time using the javac command.
You can specify multiple file names in the command line like this :
javac File1.java File2.java
From which path can we compile using javac ?
If your Java file is located in a specific package, you need to follow certain steps when compiling it using the javac command :
- Directory Structure:
Ensure that your directory structure matches your package name. For example, if your package is com.example, your directory structure should look like this :
EXAMPLE
com/
example/
MyClass.java
- Package Declaration :
At the top of your Java file (MyClass.java), make sure you have the correct package declaration :
package com.example;
- Compiling from the Parent Directory:
To compile the Java file, navigate to the parent directory of the package structure. In this case, you would navigate to the directory containing the com folder :
cd /path/to/project
- Using the javac Command :
From the parent directory, you can compile the Java file by specifying the full package path :
javac com/example/MyClass.java
From which path can we run a file using java command ?
To run a Java file using the java command, you need to consider the package structure and the classpath :
- Current Working Directory :
If your Java class is in a package, you need to be in the parent directory of that package structure. For example, if your class is MyClass in the package example, your directory structure should look like this :
EXAMPLE :
example/
MyClass.class
- Running the Class :
Navigate to the parent directory that contains the example directory. Then, you run the class using its fully qualified name :
EXAMPLE :
cd /path/to/project
java example.MyClass
- Classpath :
If your class depends on other classes or libraries, you may need to set the classpath using the -cp option:
EXAMPLE:
java -cp /path/to/libs/* example.MyClass
NOTE :
In summary, to run a Java file, you should navigate to the parent directory of the package structure where your class file is located, and then use the java command with the fully qualified class name.