u01 Java Maven Git
u01 Java Maven Git
Prof Prairie
UNC COMP 301
Fall 24
Announcements
• Gradescope code added to Canvas
• Midterm1 date is Wed, October 2nd
• My Digital Hand won’t let me register the course, so we are going to
try csxl.unc.edu, to be setup by Monday.
• My office hours are moving to Thursdays (starting Aug 29th) 1:30-4.
• Questions?
Q: What happens when you press
the “run” button in your IDE?
Q: What happens when you press
the “run” button in your IDE?
(probably not…)
Introduction
Packaging application code into a final, distributable product is a
crucial part of software development
Code is only useful if it is being used
Programming languages have different approaches to packaging and
distributing code
• Compiled languages
C, C++, Rust
Compiler parses and translates source code into machine executable code
• Must be done for the specific instruction set architecture (ISA) used by the processor
Advantage: runs fast, can be highly optimized
Disadvantage: need to create a machine specific version for every kind of machine.
Java's Big Idea
• Best of both worlds.
• Compile source code to the "machine code" of a virtual machine
Java Virtual Machine (JVM)
HelloWorldA.java
Java Development
HelloWorldA.class
package com.comp301.lec01.ex01; Kit (JDK) CA FE BA BE 00 00 00 33 00 22 0A 00 02 0
HelloWorldB.java
public class HelloWorld { HelloWorldB.class
0 03 00 04 0C 00 05 00 06 01 00 10 6A 61
76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63
public static void main(String[] args)
74 01 00 06 3C 69 6E 69 74 3E 01 00 03 2
{ package com.comp301.lec01.ex01; CA FE BA BE 00 00 00 33 00 22 0A 00 02 0
8 29 56 09 00 08 00 09 07 00 0C 00 0B 00
HelloWorldC.java
System.out.println("Hello, world!");
} public class HelloWorld {
0 03 00 04 0C 00 05 00 06 01 00 10 6A 61
HelloWorldC.class
0C 01 00 10 6A 61 76 61 2F 6C 61 2F 53
76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63
79 74 65 6D 01 00 03 6F 75 74 01
} public static void main(String[] args) 74 01 00 06 3C 69 6E 69 74 3E 01 00 03 2
{ package com.comp301.lec01.ex01; 00 15 4C 66 A0 65 63
8 29CA56FE09BA00BE080000000900073300000C22000A0B000002 0
HelloWorldD.java
System.out.println("Hello, world!");
}public class HelloWorld {
0C 01 000010046A0C6100760561002F066C0161002F10536A 61
0 03
HelloWorldD.class
79 747665616D2F016C0061036E6F67752F744F0162 6A 65 63
} public static void main(String[] args) 74 4C
01 66
00 A0
06 65
3C 63
69 6E 69 74 3E 01 00 03 2
{ package com.comp301.lec01.ex01; 00 15
8 29CA56FE 09
BA00BE080000000900073300000C22 00
0A0B0000
02 0
System.out.println("Hello, world!"); 0C00103 00
00 10
04 6A
0C 61
00 76
05 61
00 2F
06 6C
01 61
00 2F
10 53
6A 61
}
}public class HelloWorld {
{
public static void main(String[] args) Dependencies 79 74
00 74
76 65
15 01
61 6D
4C 00
2F 01
66 06
6C 00
A0 3C
61 03
65 69
6E 6F
67 75
2F 74
4F 01
62 6A 65 63
63 6E 69 74 3E 01 00 03 2
JUnit.jar
CA FE BA BE 00 00 00 33 00 22 0A 00 02 0
0 03 00 04 0C 00 05 00 06 01 00 10 6A 61
Multiple source code files JavaFX.jar
76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63
74 01 00 06 3C 69 6E 69 74 3E 01 00 03 2
CA56
8 29 FE 09
BA00BE08 000000090007
3300000C22 00
0A0B000002 0
0C00103 00
00 10
04 6A
0C 61
00 76
05 61
00 2F
06 6C
01 61
00 2F
10 53
6A 61
76 65
79 74 61 6D
2F 016C 00
61 03
6E 6F
67 75
2F 74
4F 01
62 6A 65 63
00 74
15 01
4C 00
66 06A0 3C
65 69
63 6E 69 74 3E 01 00 03 2
8 29 56 09 00 08 00 09 07 00 0C 00 0B 00
Dependencies are usually 0C 01 00 10 6A 61 76 61 2F 6C 61 2F 53
79 74 65 6D 01 00 03 6F 75 74 01
HelloWorldA.class
CA FE BA BE 00 00 00 33 00 22 0A 00 02 0
HelloWorldB.class
0 03 00 04 0C 00 05 00 06 01 00 10 6A 61 Java Virtual Machine
76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63
74 01 00 06 3C 69 6E 69 74 3E 01 00 03 2
CA FE BA BE 00 00 00 33 00 22 0A 00 02 0
8 29 56 09 00 08 00 09 07 00 0C 00 0B 00
(JVM)
0 03 00 04 0C 00 05 00 06 01 00 10 6A 61
HelloWorldC.class
0C 01 00 10 6A 61 76 61 2F 6C 61 2F 53
76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63
79 74 65 6D 01 00 03 6F 75 74 01
74 01 00 06 3C 69 6E 69 74 3E 01 00 03 2
00 15 4C 66 A0 65 63
8 29CA56FE09BA00BE080000000900073300000C22000A0B000002 0
0C 00103000010046A0C6100760561002F066C0161002F10536A 61
HelloWorldD.class
79 747665616D2F016C0061036E6F67752F744F0162 6A 65 63
74 4C
00 15 01 66
00 A0
06 65
3C 63
69 6E 69 74 3E 01 00 03 2
8 29CA56FE 09
BA00BE080000000900073300000C22 00
0A0B000002 0
0 03 00 04
0C 01 00 10 6A 61 0C 00 76
05 61
00 2F
06 6C
01 61
00 2F
10 53
6A 61
79 7476 65
61 6D
2F 01
6C 00
61 03
6E 6F
67 75
2F 74
4F 01
62 6A 65 63
00 74
15 01
4C 00
66 06
A0 3C
65 69
63 6E 69 74 3E 01 00 03 2
8 29 56 09 00 08 00 09 07 00 0C 00 0B 00
0C 01 00 10 6A 61 76 61 2F 6C 61 2F 53
Dependencies
79 74 65 6D 01 00 03 6F 75 74 01
00 15 4C 66 A0 65 63 (code you didn’t write)
JUnit.jar
CA FE BA BE 00 00 00 33 00 22 0A 00 02 0
0 03 00 04 0C 00 05 00 06 01 00 10 6A 61
JavaFX.jar
76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63
74 01 00 06 3C 69 6E 69 74 3E 01 00 03 2
CA56
8 29 FE 09
BA00BE08 000000090007
3300000C22 00
0A0B000002 0
0C00103 00
00 10
04 6A
0C 61
00 76
05 61
00 2F
06 6C
01 61
00 2F
10 53
6A 61
76 65
79 74 61 6D
2F 016C 00
61 03
6E 6F
67 75
2F 74
4F 01
62 6A 65 63
00 74
15 01
4C 00
66 06A0 3C
65 69
63 6E 69 74 3E 01 00 03 2
8 29 56 09 00 08 00 09 07 00 0C 00 0B 00
0C 01 00 10 6A 61 76 61 2F 6C 61 2F 53
79 74 65 6D 01 00 03 6F 75 74 01
00 15 4C 66 A0 65 63
Packaging a compiled Java program
Packaging a Java program A “jar” file contains the bytecode and
other assets necessary to execute the
project in a distributable format
Packaged program
Bytecode (jar file)
(compiled machine instructions)
HelloWorldA.class
CA FE BA BE 00 00 00 33 00 22 0A 00 02 0 Java Virtual Machine
HelloWorldB.class
0 03 00 04 0C 00 05 00 06 01 00 10 6A 61
76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 HelloWorld.jar (JVM)
74 01 00 06 3C 69 6E 69 74 3E 01 00 03 2
CA FE BA BE 00 00 00 33 00 22 0A 00 02 0
8 29 56 09 00 08 00 09 07 00 0C 00 0B 00
0 03 00 04 0C 00 05 00 06 01 00 10 6A 61 (JAR file)
HelloWorldC.class
0C 01 00 10 6A 61 76 61 2F 6C 61 2F 53
76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63
79 74 65 6D 01 00 03 6F 75 74 01
74 01 00 06 3C 69 6E 69 74 3E 01 00 03 2
00 15 4C 66 A0 65 63
8 29CA56FE09BA00BE080000000900073300000C22000A0B000002 0
0C 00103000010046A0C6100760561002F066C0161002F10536A 61
HelloWorldD.class
79 747665616D2F016C0061036E6F67752F744F0162 6A 65 63
74 4C
01 66
00 A0
06 65
3C 63
69 6E 69 74 3E 01 00 03 2
JAR files can be
00 15
8 29CA56FE 09
BA00BE080000000900073300000C22 00
0A0B000002 0
0 03 00 04
0C 01 00 10 6A 61 0C 00 76
05 61
00 2F
06 6C
01 61
00 2F
10 53
6A 61
76 65
61 6D
2F 01
6C 00
61 03
6E 6F
67 75
2F 74
4F 01
62 6A 65 63
executed directly
79 74
00 74
15 01
4C 00
66 06
A0 3C
65 69
63 6E 69 74 3E 01 00 03 2
8 29 56 09 00 08 00 09 07 00 0C 00 0B 00
0C 01 00 10 6A 61 76 61 2F 6C 61 2F 53
Dependencies
79 74 65 6D 01 00 03 6F 75 74 01
00 15 4C 66 A0 65 63 (code you didn’t write)
JUnit.jar
CA FE BA BE 00 00 00 33 00 22 0A 00 02 0
0 03 00 04 0C 00 05 00 06 01 00 10 6A 61
JavaFX.jar
76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63
74 01 00 06 3C 69 6E 69 74 3E 01 00 03 2
CA56
8 29 FE 09
BA00BE08 000000090007
3300000C22 00
0A0B000002 0
0C00103 00
00 10
04 6A
0C 61
00 76
05 61
00 2F
06 6C
01 61
00 2F
10 53
6A 61
76 65
79 74 61 6D
2F 016C 00
61 03
6E 6F
67 75
2F 74
4F 01
62 6A 65 63
00 74
15 01
4C 00
66 06A0 3C
65 69
63 6E 69 74 3E 01 00 03 2
8 29 56 09 00 08 00 09 07 00 0C 00 0B 00
0C 01 00 10 6A 61 76 61 2F 6C 61 2F 53
79 74 65 6D 01 00 03 6F 75 74 01
00 15 4C 66 A0 65 63
When poll is active respond at
PollEv.com/profprairie
Let’s look at the .class file!
Decompilers can get almost an exact copy of the
source code.
Compiling and executing Java
from the command line
Compiling from the command line
Terminal
“execute a Java program” List of bytecode files Full name of the class Command line
to include during execution to be executed arguments
HelloWorld lives in
https://docs.oracle.com/javase/tutorial/java/concepts/package.html
HelloWorld.java (source code)
different “units”
Put related class files in the same package
Put unrelated class files in different
packages
Subfolders implied by
package name
Which of the following is true:
Test code
(.java files)
Compiled output
(.class files)
Configuration file
Dependencies
This is an example Maven
configuration file (pom.xml)
Written in XML
Typically placed in the
root of the project folder
Specifies various settings
for the project
Project version and artifact information
Project name
Dependencies
Lifecycle configuration
Many public Java libraries provide a
Maven dependency snippet that you
can copy and paste into your
pom.xml configuration file
JavaFX has been added to pom.xml
• GitLab (self owned) also runs git in the cloud and has a different
pricing structure, more geared towards private industry usage
rather than open-opensource
Introduction
Version control is the practice of tracking changes made to a codebase
• Periodically capture a “snapshot” of the code
• Archive all snapshots taken in case you need to refer back to them
• Why is this a good idea?
• All software companies have a version control system in place
Repository
Commit Commit Commit
f22c9a3 ac901f8 d0a7bc1
dev
branch
Checkout operation
$ git checkout dev
$ git checkout ac901f8
$ git checkout main
main
branch
dev
branch
Staging and committing
Stage modified files individually $ git checkout dev
by name, or all at once with “.” (now make changes to code)
$ git add "file1.txt" "file2.txt"
$ git add .
main $ git commit –m "Commit message"
branch
dev
branch
dev
branch
Merging
main Git will try to merge the two versions
branch
automatically into a new commit, but
there might be merge conflicts that must
Commit be resolved by hand
Common ancestor 158ac9e
The main branch was merged
into the dev branch
Commit Commit Commit Commit
ac901f8 d0a7bc1 80ce97a cc917de
dev
branch
$ git checkout dev
$ git merge main
Fetching, pulling, and pushing
More git operations
Local and remote repositories
GitHub (remote)
Origin
repository
GitHub (remote)
Downloads new commits from
Origin GitHub to the local repository
repository
Does not affect files in your
working folder (HEAD)
GitHub (remote)
Uploads commits made in the
Origin local repository to GitHub
repository
repository A data structure containing an entire project, including every commit made
push The act of uploading the latest committed version of your branch to the remote machine
pull The act of integrating changes that were pushed by someone else into your local copy
merge The act of combining changes that you made with changes that someone else made
track When a local branch follows along with changes to a corresponding remote branch