Chapter 1© copyright Janson Industries Java ▮ Java programming concepts ▮ Downloading the JDK ▮ Java coding and executing example This presentation can be viewed on line at: Ch1 Assg 3
Java Concepts Chapter 1© copyright Janson Industries ▮ 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
Java Concepts Chapter 1© copyright Janson Industries Cust.java (Java file) Editor (Notepad++) Java source code Compiler Java source code Programmer javac Cust.java JVM Java bytecode Programmer java Cust main method in Machine Language CPU Cust.class (Class file)
Chapter 1© copyright Janson Industries 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 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 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 JDK (aka JSDK)
Chapter 1© copyright Janson Industries JDK on the PC ▮ Java JDK 8 is an example ▮ Use IE (not Chrome) to download the JDK: ▮ /downloads/index.html /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 (at home)
Chapter 1© copyright Janson Industries 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 Select the latest JDK version
Chapter 1© copyright Janson Industries Scroll down and click the accept agreement radio button Double click the operating system of the lab computers (Windows x86)
Chapter 1© copyright Janson Industries Specify to save the file by clicking the Save drop down button and then Save As
Chapter 1© copyright Janson Industries 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 Using My Computer open the jdkzip folder
© copyright Janson Industries 2015Chapter 115 ▮ Run the exe file by double clicking ▮ Click the Next button JDK on the PC ▮ Only takes up 180 meg ▮ Final install will take up ~ 180 meg
Chapter 1© copyright Janson Industries If you get this, you downloaded the wrong JDK
Chapter 1© copyright Janson Industries Or this (or any other msg), you downloaded the wrong JDK
Chapter 1© copyright Janson Industries Accept the default components (notice the JRE is just one part of the JDK) You can control where the JDK is installed by clicking the Change... button
Chapter 1© copyright Janson Industries 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 JDK install window redisplayed with new location Click Next
Chapter 1© copyright Janson Industries 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 Choose drive to save to, then click Make New Folder Then specify jre, click OK
Chapter 1© copyright Janson Industries JRE install window redisplayed with new location
Chapter 1© copyright Janson Industries You may have to shut down some running apps
Chapter 1© copyright Janson Industries And again…
Chapter 1© copyright Janson Industries Two folders called jdk and jre are created (verify with My Computer) Delete jdkzip\jdk-8(u#)-windows-i586-p.exe and click the Close button
Chapter 1© copyright Janson Industries 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 JDK Install Problems? ▮ Error 1723 occurred in the installer package ▮ Lot’s of possible reasons ▮ Click here for some suggestions Click here
Chapter 1© copyright Janson Industries Running a pgm ▮ When you run a 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 ▮ For instance, clicking the IE icon tells Windows to “run” iexplore.exe in C:\Program Files\Internet Explorer\
Chapter 1© copyright Janson Industries ▮ C:\Program Files\Internet Explorer\iexplore.exe is a fully qualified file name
Chapter 1© copyright Janson Industries 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 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 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 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 Will use Notepad++ to enter source code and save to a file Download Notepad++ from
Chapter 1© copyright Janson Industries Save As to your thumbdrive (so you can run anywhere)
Chapter 1© copyright Janson Industries Click Run, then OK
Chapter 1© copyright Janson Industries Click Next then I Agree
Chapter 1© copyright Janson Industries Specify a location on your thumbdrive (so you can run from anywhere) then click Next Click Next again
Chapter 1© copyright Janson Industries If installing to thumbdrive select first option, if not select third option Click Install Click Finish
Chapter 1© copyright Janson Industries Close change.log
Chapter 1© copyright Janson Industries Running a pgm Enter source code as follows (ignore colors) Then click File, Save
Chapter 1© copyright Janson Industries Start Notepad++, enter source code, and save file with type of java F:\Customer.java (must specify type as All Files) Running a pgm 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 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 To run, bring up the Command Prompt by clicking: Start, Accessories, Command Prompt Running a pgm
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
Running Chapter 1© copyright Janson Industries ▮ The current/default directory is displayed in the command prompt ▮ In this example it is H:\
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
Running Chapter 1© copyright Janson Industries Must change current directory to location of Customer.java (i.e. F:\) Customer.java is on F:\ current directory is H:\ so “file not found” Customer.java found cause default directory is F:
Chapter 1© copyright Janson Industries Possible Problems? ▮ javac or java command is not recognized ▮ Why? ▮ Customer not found: NoClassDefFoundError, ClassNotFoundException, could not find the main class ▮ 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 Current directory not where Customer file is located or file name not specified correctly (extra spaces, etc.)
Chapter 1© copyright Janson Industries 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 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
54 In Class Exercise ▮ Create Customer.java and enter the source code using notepad ▮ Compile and run Customer
55 Non-graded Assg3 ▮ Create a java file called Yay that displays the following: ▮ Yay! ▮ My first java program ▮ Send Yay.java as attachment to me at ▮ Specify the topic as Ch01Assg3
56 Non–graded Assg3 ▮ 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
Chapter 1© copyright Janson Industries
Chapter 1© copyright Janson Industries No graded assg until C2 but you can start before
Chapter 1© copyright Janson Industries The Word doc contains several non-graded assgs (labs) and one graded assg (usually the last lab in the doc)