Download ch01a.IntrotoJava

Document related concepts
no text concepts found
Transcript
Java
▮ Java programming concepts
▮ Downloading the JDK
▮ Java coding and executing example
Ch1 Assg 2
This presentation can be viewed on line at:
Chapter 1
© copyright Janson Industries 2015
1
http://web.fscj.edu/Janson/COP1000/ch01a.IntrotoJava.ppt
Java Concepts
▮ Java source code stored in files with an
extension of .java
▮ The source code is “compiled”, meaning:
▮ Translated into bytecode
▮ Bytecode stored in class files (files with an
extension of .class)
▮ Bytecode is a “cross-platform program
representation” that is non-executable
Chapter 1
© copyright Janson Industries 2015
2
Java Concepts
Cust.java
(Java file)
Cust.class
(Class file)
Java source
code
Editor
(Notepad++)
Java source
code
Programmer
Chapter 1
CPU
Java
bytecode
Compiler
javac Cust.java
Programmer
© copyright Janson Industries 2015
JVM
main method
in Machine
Language
java Cust
Programmer
3
Java Concepts
▮ Review: files with an extension of .java
contain source code
▮ Review: files with an extension of .class
contain “bytecode”
▮ .java files are “compiled” (javac command)
to create .class files
▮ Bytecode is a cross-platform program
representation that is non-executable
▮ New: bytecode needs to be translated to
machine language (java command)
Chapter 1
© copyright Janson Industries 2015
4
Java Virtual Machine (JVM)
▮ JVM is “the place” (i.e. s/w) where the
.class files (i.e. programs) are translated
▮ The JVM is invoked with the java
command
▮ The JVM translates bytecode into
machine language and sends to CPU for
execution (i.e. program is run)
▮ Therefore, each "type" of computer
needs a unique JVM
Chapter 1
© copyright Janson Industries 2015
5
Java Virtual Machine (JVM)
▮ To run a java application on a computer
you must download a JVM/JRE (java
runtime environment)
▮ But to compile you also need the java
compiler
▮ Oracle (and many other vendors) offer
▮ Oracle has a JDK (Java Development Kit)
that includes a JRE and the compiler
Chapter 1
© copyright Janson Industries 2015
6
JDK (aka JSDK)
Chapter 1
© copyright Janson Industries 2015
7
JDK on the PC
▮ Java JDK 8 is an example
▮ Use IE (not Chrome) to download the JDK:
▮ http://www.oracle.com/technetwork/java/javase
/downloads/index.html
▮ Select the latest version
▮ Click on the JDK download button for the latest
update (i.e. JDK 8, or JDK8u60, etc.)
▮ Select the JDK for your computer (i.e. Windows)
▮ Download to thumb drive (G:) or your C drive
Chapter 1 (at home)
© copyright Janson Industries 2015
8
JDK on the PC
▮ To install the JDK:
▮ Run jdk-8(u#)-windows-i586-p.exe (where # is
the latest version and update numbers)
▮ During the install, create folders to hold the JDK
and JRE called jdk and jre
▮ Delete the original .exe file jdk-8(u#)-windows-
i586-p.exe
Chapter 1
© copyright Janson Industries 2015
9
Select the latest JDK version
Chapter 1
© copyright Janson Industries 2015
10
Scroll down and click the accept agreement radio button
Double click the 32 bit operating system version
(Windows x86)
Chapter 1
© copyright Janson Industries 2015
11
Specify to save the file by clicking the Save drop down
button and then Save As
Chapter 1
© copyright Janson Industries 2015
12
Create and save to a folder called jdkzip on your thumb
drive (or the C: if you will always being work on that same
computer)
Chapter 1
© copyright Janson Industries 2015
13
Using File Explorer open the jdkzip folder
Chapter 1
© copyright Janson Industries 2015
14
JDK on the PC
▮ Only takes
up 180 meg
▮ Final install
will take up
~ 180 meg
▮ Run the exe file by
double clicking
▮ Click the Next button
Chapter 1
© copyright Janson Industries 2015
15
If you get this, you downloaded the wrong JDK
Chapter 1
© copyright Janson Industries 2015
16
Or this (or any other msg), you downloaded the wrong
JDK
Chapter 1
© copyright Janson Industries 2015
17
Accept the default components
(notice the JRE is just one part of the JDK)
Chapter 1
You can control where the JDK is installed
© copyright Janson Industries 2015
by clicking the Change... button
18
To save on typing later, we’ll change directory to just JDK i.e. F:\JDK
(or C:\JDK\) then click the OK button
Chapter 1
© copyright Janson Industries 2015
19
JDK install window redisplayed with new location
Click Next
Chapter 1
© copyright Janson Industries 2015
20
Shows where JRE will be installed. Change to…
Some students tell me this screen doesn’t appear, if it
doesn’t – no problem, skip the next two slides.
Chapter 1
© copyright Janson Industries 2015
21
Choose drive to save to, then click Make New Folder
Then specify jre, click OK
22
JRE install window redisplayed with new location
Chapter 1
© copyright Janson Industries 2015
23
You may have to shut down some running apps
Chapter 1
© copyright Janson Industries 2015
24
And again…
Chapter 1
© copyright Janson Industries 2015
25
Two folders called jdk and jre are created (verify with
File Explorer)
Delete jdkzip\jdk-8(u#)-windows-i586-p.exe and
click the Close button
Chapter 1
© copyright Janson Industries 2015
26
To verify that you installed correctly, display the
contents of jdk/bin folder and make sure you have two
executable files called javac and java
Chapter 1
© copyright Janson Industries 2015
27
JDK Install Problems?
▮ Error 1723 occurred in the installer
package
▮ Lot’s of possible reasons
▮ Click here for some suggestions
Chapter 1
© copyright Janson Industries 2015
28
Running a pgm
▮ When you run an application in
Windows, you are specifying an
executable file name
▮ Executable files contain machine
language
▮ On Windows machines these files have
names that end in .exe
Chapter 1
© copyright Janson Industries 2015
29
Running a pgm
▮ For instance, clicking
the IE icon tells
Windows to “run”
iexplore.exe in
C:\Program
Files\Internet Explorer\
Chapter 1
© copyright Janson Industries 2015
30
▮ C:\Program Files\Internet Explorer\iexplore.exe is a
fully qualified file name
Chapter 1
© copyright Janson Industries 2015
31
Running a pgm
▮ If a fully qualified file name is not
specified, Windows searches
▮ 1. The default (aka current) directory
▮ 2. The locations stored in the the
Environment Variable called PATH
▮ I.e. path holds all the locations Windows
should search when an unqualified file name
is specified
▮ You can control the value of the Path variable
▮ Let’s prove it!!
Chapter 1
© copyright Janson Industries 2015
32
Running a pgm
▮ Let’s prove it
▮ In command prompt we specify iexplore.exe
▮ “Not recognized” message
▮ Now specify fully qualified file name (in quotes)
▮ IE started
Chapter 1
© copyright Janson Industries 2015
33
Running a pgm
▮ Now we will set the path value
▮ path = "C:\Program Files\Internet Explorer\"
▮ In command prompt we specify iexplore.exe
▮ IE started
Chapter 1
© copyright Janson Industries 2015
34
Running a pgm
▮ JDK must be in the PC’s path to access the
compile (javac) and run (java) commands
(as well as other stuff)
▮ Path holds locations of executable files
▮ Several ways to define path locations
▮ Each time you go to the command prompt
(depending on where jdk installed) enter
▮ path=f:\jdk\bin
or
▮ path=c:\jdk\bin
Chapter 1
© copyright Janson Industries 2015
35
Will use Notepad++ to enter source code and save to a file
Download Notepad++ from http://notepad-plus-plus.org/download
Chapter 1
© copyright Janson Industries 2015
36
Save As to your thumbdrive (so you can run anywhere)
Chapter 1
© copyright Janson Industries 2015
37
Click Run, then OK
Chapter 1
© copyright Janson Industries 2015
38
Click Next then I Agree
Chapter 1
© copyright Janson Industries 2015
39
Specify a location on your thumbdrive (so you can run from
anywhere) then click Next
Click Next again
Chapter 1
© copyright Janson Industries 2015
40
If installing to thumbdrive select first option, if not select third option
Click Install
Click Finish
Chapter 1
© copyright Janson Industries 2015
41
Close change.log
Chapter 1
© copyright Janson Industries 2015
42
Enter source code as follows (ignore colors)
Then click File, Save
Running a pgm
Chapter 1
© copyright Janson Industries 2015
43
Java Rules
▮ File name (Customer) has to match class name
(Customer)
▮ Executable statements ended with a semicolon
(;)
▮ System.out.println not System.out.printIn
▮ Stands for “print line” not “print In”.
▮ Open symbols - ( { [ - must have matching
closing symbols - ) } ].
Chapter 1
© copyright Janson Industries 2015
44
Running a pgm
Start Notepad++, enter source code, and save file with type of java
F:\Customer.java (must specify type as All Files)
Save to the root of your thumbdrive (i.e. F:\)
Change Save as type to Java source file then the file name to
Customer.java (must leave .java in file name)
Click Save
Chapter 1
© copyright Janson Industries 2015
45
Running a pgm
A couple java rules:
File name (Customer) must match certain statements
System.out.println() displays text
Statements end in a semicolon (;)
Chapter 1
© copyright Janson Industries 2015
46
Running a pgm
Bring up the Command Prompt by clicking:
Start, All Apps, Windows System, Command Prompt
Chapter 1
© copyright Janson Industries 2016
47
Running
▮ When javac and java commands are run, at
first they do the same thing as Windows
▮ They check the default directory for the .java
(source code) and .class (bytecode) files
▮ If files not there, "file not found" msg displayed
▮ Solution
▮ Change current directory to the location of the files
with the CD (change directory) command
▮ CD F:\, hit Enter, then type F:, hit Enter again
Chapter 1
© copyright Janson Industries 2015
48
Running
▮ The current/default directory is displayed in the
command prompt
▮ In this example it is H:\
Chapter 1
© copyright Janson Industries 2015
49
Running
▮ 1. Issue the path command so that jdk/bin is in
the path
▮ 2. If necessary, change the default path to the
location of the .java file to be compiled (i.e. F:\ )
(type CD F:\, hit Enter, then type F:, hit Enter)
▮ 3. Convert to byte code (javac Customer.java)
▮ 4. Run the program (java Customer)
Chapter 1
© copyright Janson Industries 2015
50
Running
Customer.java is on F:\
current directory is H:\
so “file not found”
Must change current directory to
location of Customer.java (i.e. F:\)
Customer.java found cause
default directory is F:
Chapter 1
© copyright Janson Industries 2015
51
Possible Problems?
▮ javac or java command is not recognized
▮ Why?
Path not defined correctly. Common mistakes: wrong
drive letter, incorrect directory name(s), extra spaces after
bin, when installing you mixed up jdk and jre directories
▮ Customer not found:
NoClassDefFoundError,
ClassNotFoundException, could not find the main class
▮ Why?
Chapter 1
Current directory not where Customer file is located or file
name not specified correctly (extra spaces, etc.)
© copyright Janson Industries 2015
52
Possible Problems?
▮ When there are syntax errors, JVM will
show:
▮ A message with useful info
▮ File name: Customer.java
▮ Line number that has the error: 10
▮ What the problem is: ‘;’ expected
▮ The source code statements that have errors
with ^ under where it thinks there are problems
Chapter 1
© copyright Janson Industries 2015
53
Possible Problems?
▮ Java syntax errors
▮ Upper case/lower case letters are
different
▮ If example shows upper case letter, you must
use upper case letter
▮ Forward slash/back slash are different
▮ println (means: print line) not printIn
Chapter 1
© copyright Janson Industries 2015
54
In Class Exercise
▮ Create Customer.java and enter the
source code using notepad
▮ Compile and run Customer
55
Non-graded Assg2
▮ Create a java file called Yay that displays
the following:
▮ Yay!
▮ My first java program
▮ Send Yay.java as email attachment to me
at [email protected]
▮ Specify the Email topic as Ch01Assg2
56
Non–graded Assg2
▮ When Yay is run the output should look
like this:
57
Graded Assignments
▮ You will have to download lab files (Word
documents) for the graded assignments
▮ Create a folder on your memory stick to
hold the files (and your work) called
COP1000
▮ Download the labs from the the folder
Labs at the same location as the PP
presentations
58
Java
▮ Java Language Companion (JLC) guide
▮ This is the Java text book
▮ In a PDF file at the same location as the
PP presentations and labs
▮ Download and read
59
http://web.fscj.edu/Janson/COP1000/
Chapter 1
© copyright Janson Industries 2015
60
No graded assg until C2 but you can start
before
Chapter 1
© copyright Janson Industries 2015
61
The Word doc contains several non-graded
assgs (labs) and one graded assg
(usually the last lab in the doc)
Chapter 1
© copyright Janson Industries 2015
62