SlideShare a Scribd company logo
1 of 16
Development

1
Using the javac and java Commands
(Objective 7.2)

• 7.1 Given a code example and a scenario, write code that
uses the appropriate access modifiers, package
declarations, and import statements to interact with
(through access or inheritance) the code in the example.
• 7.2 Given an example of a class and a commandline, determine the expected runtime behavior.
• 7.5 Given the fully-qualified name of a class that is
deployed inside and/or outside a JAR file, construct the
appropriate directory structure for that class. Given a
code example and a classpath, determine whether the
classpath will allow the code to compile successfully.
2
Compiling with javac

• javac [options] [source files]
• javac -help
• javac -classpath com:. -g Foo.java
Bar.java
• Whenever you specify multiple options
and/or files they should be separated by
spaces.
3
Compiling with -d
• By default, the compiler puts a .class file in the same
directory as the .java source file
> This is fine for very small projects

• The -d option lets you tell the compiler in which directory
to put the .class file(s) it generates (d is for destination)
myProject
|
|--source
||
| |-- MyClass.java
|
|-- classes
|
|--

cd myProject
javac -d classes source/MyClass.java

4
myProject
|
|--source
|
|
|
|--com
|
|
|
|--wickedlysmart
|
|
|
|--MyClass.java
|
|--classes
|
|

• In this case, the compiler will build two directories called com and
com/wickedlysmart in order to put the resulting MyClass.class file into the correct package
directory :

5
• The last thing about -d that you'll need to know for the
exam is that if the destination directory you specify
doesn't exist, you'll get a compiler error.
• If, in the previous example, the classes directory did NOT
exist, the compiler would say something like:
• java:5: error while writing MyClass:
classes/MyClass.class (No such file or directory)

6
Launching Applications with java
• In Chapter 5 we talked about the assertion mechanism
and when you might use flags such as -ea or -da when
launching an application.

• java [options] class [args]
java -DmyProp=myValue MyClass x 1
• Sparing the details for later, this command can be read as "Create a system property called
myProp and set its value to myValue. Then launch the file named MyClass.
• class and send it two String arguments whose values are x and 1."

7
Using System Properties

If this file is compiled and invoked
as follows:
import java.util.*;
java -DcmdProp=cmdVal TestProps
public class TestProps {
public static void main(String[] args) { You'll get something like this:
...
Properties p =
os.name=Mac OS X
System.getProperties();
myProp=myValue
p.setProperty("myProp", "myValue");
...
p.list(System.out);
java.specification.vendor=Sun Microsystems Inc.
}
user.language=en
java.version=1.5.0_02
}
...
cmdProp=cmdVal
...

8
• When using the -D option, if your value contains white
space the entire value should be placed in quotes like
this:
• java -DcmdProp="cmdVal take 2" TestProps

9
Handling Command-Line Arguments
public class CmdArgs {
compiled and then invoked as follows
public static void main(String[]
java CmdArgs x 1
args) {
the output will be
int x = 0;
0 element = x
for(String s : args)
1 element = 1
System.out.println(x++ + "
element = " + s);
}
} The following are all legal declarations for main():
static public void main(String[] args)
public static void main(String... x)
static public void main(String bang_a_gong[])
10
Searching for Other Classes
Both java and javac use the same basic search algorithm:
1. They both have the same list of places (directories) they
search, to look for classes.
2. They both search through this list of directories in the same
order.
3. As soon as they find the class they're looking for, they stop
searching for that class. In the case that their search lists
contain two or more files with the same name, the first file
found will be the file that is used.
4. The first place they look is in the directories that contain the
classes that come standard with J2SE.
11
5. The second place they look is in the directories defined by
classpaths.
6. Classpaths should be thought of as "class search paths." They
are lists of directories in which classes might be found.
7. There are two places where classpaths can be declared:
> A classpath can be declared as an operating system environment

variable. The classpath declared here is used by default, whenever java
or javac are invoked.

> A classpath can be declared as a command-line option for either java or

javac. Classpaths declared as command-line options override the
classpath declared as an environment variable, but they persist only for
the length of the invocation.
12
Declaring and Using Classpaths
• -classpath /com/foo/acct:/com/foo
• specifies two directories in which classes can be found:
/com/foo/acct and /com/foo
• the java and javac commands don't search the current
directory by default. You must tell them to search there.

:

.

• -classpath /com/foo/acct /com/foo:

13
It's also important to remember that
classpaths are searched from left to right.

•-classpath /com:/foo:.
•is not the same as
•-classpath .:/foo:/com
• Finally, the java command allows you to abbreviate classpath with -cp
14
Packages and Searching
import com.wickedlysmart.Utils;
class TestClass {
void doStuff() {
Utils u = new Utils(); // simple name
u.doX("arg1", "arg2");
com.wickedlysmart.Date d =
new com.wickedlysmart.Date(); // full name
d.getMonth("Oct");
}
}

15
16

More Related Content

What's hot (18)

Packages and interfaces
Packages and interfacesPackages and interfaces
Packages and interfaces
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java - Packages Concepts
Java - Packages ConceptsJava - Packages Concepts
Java - Packages Concepts
 
Package in java
Package in javaPackage in java
Package in java
 
Java package
Java packageJava package
Java package
 
Package In Java
Package In JavaPackage In Java
Package In Java
 
Android datastorage
Android datastorageAndroid datastorage
Android datastorage
 
Sql lite android
Sql lite androidSql lite android
Sql lite android
 
Packages(9 cm604.26)
Packages(9 cm604.26)Packages(9 cm604.26)
Packages(9 cm604.26)
 
Javapackages 4th semester
Javapackages 4th semesterJavapackages 4th semester
Javapackages 4th semester
 
Mule system properties
Mule system propertiesMule system properties
Mule system properties
 
Android - Data Storage
Android - Data StorageAndroid - Data Storage
Android - Data Storage
 
Filter
FilterFilter
Filter
 
Android Data Storagefinal
Android Data StoragefinalAndroid Data Storagefinal
Android Data Storagefinal
 
Ldapsession 1217528612650451-9
Ldapsession 1217528612650451-9Ldapsession 1217528612650451-9
Ldapsession 1217528612650451-9
 
Ldapsession
LdapsessionLdapsession
Ldapsession
 
Advanced R cheat sheet
Advanced R cheat sheetAdvanced R cheat sheet
Advanced R cheat sheet
 
Jdbc[1]
Jdbc[1]Jdbc[1]
Jdbc[1]
 

Viewers also liked

chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)It Academy
 
Pp orientació mediterrània
Pp orientació mediterràniaPp orientació mediterrània
Pp orientació mediterràniamnogue47
 
Les communes concernées par la pollution en Isère
Les communes concernées par la pollution en IsèreLes communes concernées par la pollution en Isère
Les communes concernées par la pollution en IsèreSarah Rebouh
 
Bad brews night 02.25.2010
Bad brews night 02.25.2010Bad brews night 02.25.2010
Bad brews night 02.25.2010Lucas Wolf
 
Florian Guanio
Florian GuanioFlorian Guanio
Florian Guanioflguanio
 
Volem ser escola verda adults
Volem ser escola verda adultsVolem ser escola verda adults
Volem ser escola verda adultsmnogue47
 
chap 6 : Objects and classes (scjp/ocjp)
chap 6 : Objects and classes (scjp/ocjp)chap 6 : Objects and classes (scjp/ocjp)
chap 6 : Objects and classes (scjp/ocjp)It Academy
 
Bad brews night 02.25.2010
Bad brews night 02.25.2010Bad brews night 02.25.2010
Bad brews night 02.25.2010Lucas Wolf
 
Florian Guanio
Florian GuanioFlorian Guanio
Florian Guanioflguanio
 
Chapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic ConceptsChapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic ConceptsIt Academy
 
Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)It Academy
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsIt Academy
 
Genética de la violencia: Gen MAOA
Genética de la violencia: Gen MAOAGenética de la violencia: Gen MAOA
Genética de la violencia: Gen MAOAJoel Ricci-López
 
6 softwares de manutenção
6 softwares de manutenção6 softwares de manutenção
6 softwares de manutençãoMauro Pereira
 

Viewers also liked (19)

chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)chp 3 : Modifiers (scjp/ocjp)
chp 3 : Modifiers (scjp/ocjp)
 
Pp orientació mediterrània
Pp orientació mediterràniaPp orientació mediterrània
Pp orientació mediterrània
 
2.D
2.D2.D
2.D
 
Les communes concernées par la pollution en Isère
Les communes concernées par la pollution en IsèreLes communes concernées par la pollution en Isère
Les communes concernées par la pollution en Isère
 
Bad brews night 02.25.2010
Bad brews night 02.25.2010Bad brews night 02.25.2010
Bad brews night 02.25.2010
 
Florian Guanio
Florian GuanioFlorian Guanio
Florian Guanio
 
Volem ser escola verda adults
Volem ser escola verda adultsVolem ser escola verda adults
Volem ser escola verda adults
 
Slides show
Slides showSlides show
Slides show
 
2.J
2.J2.J
2.J
 
chap 6 : Objects and classes (scjp/ocjp)
chap 6 : Objects and classes (scjp/ocjp)chap 6 : Objects and classes (scjp/ocjp)
chap 6 : Objects and classes (scjp/ocjp)
 
Filiz özyaprak
Filiz özyaprakFiliz özyaprak
Filiz özyaprak
 
Bad brews night 02.25.2010
Bad brews night 02.25.2010Bad brews night 02.25.2010
Bad brews night 02.25.2010
 
Florian Guanio
Florian GuanioFlorian Guanio
Florian Guanio
 
Chapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic ConceptsChapter 4:Object-Oriented Basic Concepts
Chapter 4:Object-Oriented Basic Concepts
 
Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)Chap1 language fondamentale of java ( scjp /ocjp)
Chap1 language fondamentale of java ( scjp /ocjp)
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
2.N
2.N2.N
2.N
 
Genética de la violencia: Gen MAOA
Genética de la violencia: Gen MAOAGenética de la violencia: Gen MAOA
Genética de la violencia: Gen MAOA
 
6 softwares de manutenção
6 softwares de manutenção6 softwares de manutenção
6 softwares de manutenção
 

Similar to Use javac and java commands to compile and run Java code

Similar to Use javac and java commands to compile and run Java code (20)

Java basics
Java basicsJava basics
Java basics
 
API workshop: Deep dive into Java
API workshop: Deep dive into JavaAPI workshop: Deep dive into Java
API workshop: Deep dive into Java
 
Java Class Loading
Java Class LoadingJava Class Loading
Java Class Loading
 
Arquillian in a nutshell
Arquillian in a nutshellArquillian in a nutshell
Arquillian in a nutshell
 
Classpath
ClasspathClasspath
Classpath
 
Advance java kvr -satya
Advance java  kvr -satyaAdvance java  kvr -satya
Advance java kvr -satya
 
Adv kvr -satya
Adv  kvr -satyaAdv  kvr -satya
Adv kvr -satya
 
Java Packages
Java Packages Java Packages
Java Packages
 
20-packages-jar.ppt
20-packages-jar.ppt20-packages-jar.ppt
20-packages-jar.ppt
 
Java Class Loader
Java Class LoaderJava Class Loader
Java Class Loader
 
Dspace4 150227090306-conversion-gate01
Dspace4 150227090306-conversion-gate01Dspace4 150227090306-conversion-gate01
Dspace4 150227090306-conversion-gate01
 
DSpace 4.2 Basics & Configuration
DSpace 4.2 Basics & ConfigurationDSpace 4.2 Basics & Configuration
DSpace 4.2 Basics & Configuration
 
javapackage
javapackagejavapackage
javapackage
 
5 the final_hard_part
5 the final_hard_part5 the final_hard_part
5 the final_hard_part
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
 
Java programming basics
Java programming basicsJava programming basics
Java programming basics
 
Advanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sirAdvanced java jee material by KV Rao sir
Advanced java jee material by KV Rao sir
 
Java 8 in Anger (QCon London)
Java 8 in Anger (QCon London)Java 8 in Anger (QCon London)
Java 8 in Anger (QCon London)
 
Java 8 in Anger, Devoxx France
Java 8 in Anger, Devoxx FranceJava 8 in Anger, Devoxx France
Java 8 in Anger, Devoxx France
 
Java packages
Java packagesJava packages
Java packages
 

More from It Academy

Chapter 12:Understanding Server-Side Technologies
Chapter 12:Understanding Server-Side TechnologiesChapter 12:Understanding Server-Side Technologies
Chapter 12:Understanding Server-Side TechnologiesIt Academy
 
Chapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration TechnologiesChapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration TechnologiesIt Academy
 
Chapter 11:Understanding Client-Side Technologies
Chapter 11:Understanding Client-Side TechnologiesChapter 11:Understanding Client-Side Technologies
Chapter 11:Understanding Client-Side TechnologiesIt Academy
 
Chapter 9:Representing Object-Oriented Concepts with UML
Chapter 9:Representing Object-Oriented Concepts with UMLChapter 9:Representing Object-Oriented Concepts with UML
Chapter 9:Representing Object-Oriented Concepts with UMLIt Academy
 
Chapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismChapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismIt Academy
 
Chapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceChapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceIt Academy
 
Chapter 6:Working with Classes and Their Relationships
Chapter 6:Working with Classes and Their RelationshipsChapter 6:Working with Classes and Their Relationships
Chapter 6:Working with Classes and Their RelationshipsIt Academy
 
Chapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionChapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionIt Academy
 
Chapter 3:Programming with Java Operators and Strings
Chapter 3:Programming with Java Operators and  StringsChapter 3:Programming with Java Operators and  Strings
Chapter 3:Programming with Java Operators and StringsIt Academy
 
Chapter 3 : Programming with Java Operators and Strings
Chapter 3 : Programming with Java Operators and  StringsChapter 3 : Programming with Java Operators and  Strings
Chapter 3 : Programming with Java Operators and StringsIt Academy
 
Chap 9 : I/O and Streams (scjp/ocjp)
Chap 9 : I/O and Streams (scjp/ocjp)Chap 9 : I/O and Streams (scjp/ocjp)
Chap 9 : I/O and Streams (scjp/ocjp)It Academy
 
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)It Academy
 
chap 7 : Threads (scjp/ocjp)
chap 7 : Threads (scjp/ocjp)chap 7 : Threads (scjp/ocjp)
chap 7 : Threads (scjp/ocjp)It Academy
 
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)It Academy
 
chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)It Academy
 
chap 2 : Operators and Assignments (scjp/ocjp)
chap 2 : Operators and Assignments (scjp/ocjp)chap 2 : Operators and Assignments (scjp/ocjp)
chap 2 : Operators and Assignments (scjp/ocjp)It Academy
 
Design patterns gof fr
Design patterns gof frDesign patterns gof fr
Design patterns gof frIt Academy
 

More from It Academy (19)

Chapter 12:Understanding Server-Side Technologies
Chapter 12:Understanding Server-Side TechnologiesChapter 12:Understanding Server-Side Technologies
Chapter 12:Understanding Server-Side Technologies
 
Chapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration TechnologiesChapter 10:Understanding Java Related Platforms and Integration Technologies
Chapter 10:Understanding Java Related Platforms and Integration Technologies
 
Chapter 11:Understanding Client-Side Technologies
Chapter 11:Understanding Client-Side TechnologiesChapter 11:Understanding Client-Side Technologies
Chapter 11:Understanding Client-Side Technologies
 
Chapter 9:Representing Object-Oriented Concepts with UML
Chapter 9:Representing Object-Oriented Concepts with UMLChapter 9:Representing Object-Oriented Concepts with UML
Chapter 9:Representing Object-Oriented Concepts with UML
 
Chapter8:Understanding Polymorphism
Chapter8:Understanding PolymorphismChapter8:Understanding Polymorphism
Chapter8:Understanding Polymorphism
 
Chapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class InheritanceChapter 7:Understanding Class Inheritance
Chapter 7:Understanding Class Inheritance
 
Chapter 6:Working with Classes and Their Relationships
Chapter 6:Working with Classes and Their RelationshipsChapter 6:Working with Classes and Their Relationships
Chapter 6:Working with Classes and Their Relationships
 
Chapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class ConstructionChapter 5:Understanding Variable Scope and Class Construction
Chapter 5:Understanding Variable Scope and Class Construction
 
Chapter 3:Programming with Java Operators and Strings
Chapter 3:Programming with Java Operators and  StringsChapter 3:Programming with Java Operators and  Strings
Chapter 3:Programming with Java Operators and Strings
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Chapter 3 : Programming with Java Operators and Strings
Chapter 3 : Programming with Java Operators and  StringsChapter 3 : Programming with Java Operators and  Strings
Chapter 3 : Programming with Java Operators and Strings
 
Chapter 1 :
Chapter 1 : Chapter 1 :
Chapter 1 :
 
Chap 9 : I/O and Streams (scjp/ocjp)
Chap 9 : I/O and Streams (scjp/ocjp)Chap 9 : I/O and Streams (scjp/ocjp)
Chap 9 : I/O and Streams (scjp/ocjp)
 
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)chap 8 : The java.lang and java.util Packages (scjp/ocjp)
chap 8 : The java.lang and java.util Packages (scjp/ocjp)
 
chap 7 : Threads (scjp/ocjp)
chap 7 : Threads (scjp/ocjp)chap 7 : Threads (scjp/ocjp)
chap 7 : Threads (scjp/ocjp)
 
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
chap4 ; Flow Control, Assertions, and Exception Handling (scjp/ocjp)
 
chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)chap4 : Converting and Casting (scjp/ocjp)
chap4 : Converting and Casting (scjp/ocjp)
 
chap 2 : Operators and Assignments (scjp/ocjp)
chap 2 : Operators and Assignments (scjp/ocjp)chap 2 : Operators and Assignments (scjp/ocjp)
chap 2 : Operators and Assignments (scjp/ocjp)
 
Design patterns gof fr
Design patterns gof frDesign patterns gof fr
Design patterns gof fr
 

Recently uploaded

Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 

Recently uploaded (20)

Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Panjabi Bagh 🔝 9953056974 🔝 Delhi escort Service
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 

Use javac and java commands to compile and run Java code

  • 2. Using the javac and java Commands (Objective 7.2) • 7.1 Given a code example and a scenario, write code that uses the appropriate access modifiers, package declarations, and import statements to interact with (through access or inheritance) the code in the example. • 7.2 Given an example of a class and a commandline, determine the expected runtime behavior. • 7.5 Given the fully-qualified name of a class that is deployed inside and/or outside a JAR file, construct the appropriate directory structure for that class. Given a code example and a classpath, determine whether the classpath will allow the code to compile successfully. 2
  • 3. Compiling with javac • javac [options] [source files] • javac -help • javac -classpath com:. -g Foo.java Bar.java • Whenever you specify multiple options and/or files they should be separated by spaces. 3
  • 4. Compiling with -d • By default, the compiler puts a .class file in the same directory as the .java source file > This is fine for very small projects • The -d option lets you tell the compiler in which directory to put the .class file(s) it generates (d is for destination) myProject | |--source || | |-- MyClass.java | |-- classes | |-- cd myProject javac -d classes source/MyClass.java 4
  • 5. myProject | |--source | | | |--com | | | |--wickedlysmart | | | |--MyClass.java | |--classes | | • In this case, the compiler will build two directories called com and com/wickedlysmart in order to put the resulting MyClass.class file into the correct package directory : 5
  • 6. • The last thing about -d that you'll need to know for the exam is that if the destination directory you specify doesn't exist, you'll get a compiler error. • If, in the previous example, the classes directory did NOT exist, the compiler would say something like: • java:5: error while writing MyClass: classes/MyClass.class (No such file or directory) 6
  • 7. Launching Applications with java • In Chapter 5 we talked about the assertion mechanism and when you might use flags such as -ea or -da when launching an application. • java [options] class [args] java -DmyProp=myValue MyClass x 1 • Sparing the details for later, this command can be read as "Create a system property called myProp and set its value to myValue. Then launch the file named MyClass. • class and send it two String arguments whose values are x and 1." 7
  • 8. Using System Properties If this file is compiled and invoked as follows: import java.util.*; java -DcmdProp=cmdVal TestProps public class TestProps { public static void main(String[] args) { You'll get something like this: ... Properties p = os.name=Mac OS X System.getProperties(); myProp=myValue p.setProperty("myProp", "myValue"); ... p.list(System.out); java.specification.vendor=Sun Microsystems Inc. } user.language=en java.version=1.5.0_02 } ... cmdProp=cmdVal ... 8
  • 9. • When using the -D option, if your value contains white space the entire value should be placed in quotes like this: • java -DcmdProp="cmdVal take 2" TestProps 9
  • 10. Handling Command-Line Arguments public class CmdArgs { compiled and then invoked as follows public static void main(String[] java CmdArgs x 1 args) { the output will be int x = 0; 0 element = x for(String s : args) 1 element = 1 System.out.println(x++ + " element = " + s); } } The following are all legal declarations for main(): static public void main(String[] args) public static void main(String... x) static public void main(String bang_a_gong[]) 10
  • 11. Searching for Other Classes Both java and javac use the same basic search algorithm: 1. They both have the same list of places (directories) they search, to look for classes. 2. They both search through this list of directories in the same order. 3. As soon as they find the class they're looking for, they stop searching for that class. In the case that their search lists contain two or more files with the same name, the first file found will be the file that is used. 4. The first place they look is in the directories that contain the classes that come standard with J2SE. 11
  • 12. 5. The second place they look is in the directories defined by classpaths. 6. Classpaths should be thought of as "class search paths." They are lists of directories in which classes might be found. 7. There are two places where classpaths can be declared: > A classpath can be declared as an operating system environment variable. The classpath declared here is used by default, whenever java or javac are invoked. > A classpath can be declared as a command-line option for either java or javac. Classpaths declared as command-line options override the classpath declared as an environment variable, but they persist only for the length of the invocation. 12
  • 13. Declaring and Using Classpaths • -classpath /com/foo/acct:/com/foo • specifies two directories in which classes can be found: /com/foo/acct and /com/foo • the java and javac commands don't search the current directory by default. You must tell them to search there. : . • -classpath /com/foo/acct /com/foo: 13
  • 14. It's also important to remember that classpaths are searched from left to right. •-classpath /com:/foo:. •is not the same as •-classpath .:/foo:/com • Finally, the java command allows you to abbreviate classpath with -cp 14
  • 15. Packages and Searching import com.wickedlysmart.Utils; class TestClass { void doStuff() { Utils u = new Utils(); // simple name u.doX("arg1", "arg2"); com.wickedlysmart.Date d = new com.wickedlysmart.Date(); // full name d.getMonth("Oct"); } } 15
  • 16. 16