1.4-2_Creating and Compiling Packages Flashcards
If no package defined, where is the code placed?
When NO package statement present, the code is placed in what is known as the “default package”.
Which strategy should we use to avoid naming conflicts and reuse of your code?
Always name your packages
How to compile ClassA and ClassB via terminal? ├── packagea │ ├── ClassA.class │ └── ClassA.java └── packageb ├── ClassB.class └── ClassB.java
$ javac packagea/ClassA.java packageb/ClassB.java
🤯️Notice that when defining a package we use dot notation.
⚠️When compiling we use / (forward slash) referencing the directory structure.
How to compile packages using wildcards?
$ javac packagea/.java packageb/.java
What is the difference between path & classpath?
PATH is the environment variable where we specify the locations of binaries.
CLASSPATH is the path for Java application where the classes you compiled will be available.
How to use an alternate target directory when compiling?
$ javac -d classes packagea/MyClass.java When compiling the -d option specifies the target directory for placing the .class files, otherwise it will be placed in the same path of the class being compiled. ⚠️ Java options are case sensitive. This means you cannot pass -D instead of -d.
What does indicate the period (.) when included as a directory in the classpath?
The period (.) indicates you want to include the current directory in the classpath.
How to compile with JAR files?
$jar -cvf myNewFile.jar .
$jar –create –verbose –file myNewFile.jar .
(-c | –create) Create
(-v | –versose) Print details
(-f | –filename) JAR filename
(-C) Target directory to create the JAR from
How to specify a Target Directory when creating a JAR?
(-C) Target directory to create the JAR from
jar -cvf myNewFile.jar -C dir .