Imports and Build Systems
IntelliJ projects for today’s lecture:
> Click or hit Control-Enter to run the code above
Analyzing Your Solution
-
What was your algorithm?
-
What was the big-O runtime of your implementation?
-
What are the worst- and best-case inputs for your implementation?
I promised that…
you’d understand every line of a typical Java source file.
Java Packages
A Java package is a bundle of resources—mainly classes—that provide related functionality.
-
The Java SDK provides a set of packages starting with
java
orjavax
: likejava.util
. -
These standard libraries are always available for you to use when you write Java code.
-
Other companies and organizations can create and distribute their own software packages: like
com.google.common.math
ororg.apache.commons.text
. -
Unlike the standard libraries, you have to do some extra work to make these libraries available for each project you are working on.
Package Naming
Java establishes naming conventions to ensure that multiple developers can work on similar projects without collisions.
-
Standard libraries start with
java
orjavax
-
External libraries use a reversed domain name:
com.google…
,edu.illinois.cs.cs125….
, orcom.github.cs125-illinois…
. -
If you publish to GitHub,
com.github.<username>…
is a good choice.
Don’t Do It Yourself
You have to learn to use software libraries.
Course v. Real Programming
Course | Real | |
---|---|---|
You start with… |
A small amount of great code |
A large amount of complex and confusing code |
Most of your time is spent… |
Programming |
Understanding the system |
You solve problems… |
Yourself! |
By finding existing solutions |
You end up building… |
Something small |
Something really cool |
We call this… |
Doing the MP |
Hacking |
Importing Standard Libraries
// If I use the fully-qualified name I don't need an import...
System.out.println(java.util.Arrays.toString(new int[] { 1, 2, 3 }));
// ...but it's common to use one.
// This makes Arrays available in this class
import java.util.Arrays;
// Now I can use its methods
System.out.println(Arrays.toString(new int[] { 1, 2, 3 }));
// I can also import all classes from java.util, although
// checkstyle will complain
import java.util.*;
// Now I can use its methods
System.out.println(Arrays.toString(new int[] { 1, 2, 3 }));
Importing Non-Standard Libraries
// Unfortunately, it's not quite this easy...
import com.google.common.math.BigIntegerMath;
Gradle to the Rescue
There are multiple ways to make external libraries available as part of your project. We’re going to show you how to do this using Gradle.
What is Gradle 1?
Gradle is a build tool. It helps with things like:
-
building your Java program or Android app
-
running test suites and code quality tools like
checkstyle
-
packaging your Java code into a package, if appropriate
-
build Javadoc documentation
-
fetching any dependencies that your project needs
Example build.gradle
// We're building a Java application
apply plugin: 'java'
// Look for dependencies on jcenter
repositories {
jcenter()
}
// We don't have any dependencies yet
dependencies {
}
IntelliJ Example Using Google’s Guava Library
-
Link to Google’s Documentation: https://github.com/google/guava
-
Link to starter code: https://github.com/cs125-illinois/lecture-packaging-app
build.gradle
With Dependency
// We're building a Java application
apply plugin: 'java'
// Look for dependencies on jcenter
repositories {
jcenter()
}
// Use the Google Guava library version 24.1-jre
dependencies {
// When I compile the project I need this library
compile 'com.google.guava:guava:24.1-jre'
}
Gradle Dependency Format
dependencies {
compile 'com.google.guava:guava:24.1-jre'
// example: compile 'GROUP:ARTIFACT:VERSION'
}
Gradle’s dependency block breaks each dependency into three parts:
-
GROUP
: usually indicates an organization that provides this dependency, likecom.google
orcom.github.cs125-illinois
. -
ARTIFACT
: a string identifying the dependency to add to the project. -
VERSION
: a version string identifying which version of the library to use.
Maven Format
Sometimes you see the dependency in this format, which is used by another build tool called Maven:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>1.3</version>
</dependency>
This is equivalent to org.apache.commons:commons-text:1.3
in Gradle.
GROUP
and ARTIFACT
Just be aware that these are not necessarily related to the package name.
-
com.google.guava:guava
providescom.google.common.math
, etc. -
com.github.cs125-illinois:sortlib
providesedu.illinois.cs.cs125.sortlib
VERSION
Package versioning is incredibly important.
-
If you develop your app to work with
1.10.1
, it may not work with2.0.0
. -
So you pin your app at a particular version of a library.
-
However, it is important to periodically update your libraries to the latest version, but not that that may take some time and energy.
.jar
Files
A single Java library usually consists of many different class files and other
resources. They are packaged into a single Java archive, or .jar
file.
$ jar tvf sortlib.jar
0 Sun Apr 15 16:00:08 CDT 2018 META-INF/
25 Sun Apr 15 16:00:08 CDT 2018 META-INF/MANIFEST.MF
0 Sun Apr 15 16:00:08 CDT 2018 edu/
0 Sun Apr 15 16:00:08 CDT 2018 edu/illinois/
0 Sun Apr 15 16:00:08 CDT 2018 edu/illinois/cs/
0 Sun Apr 15 16:00:08 CDT 2018 edu/illinois/cs/cs125/
0 Sun Apr 15 16:00:08 CDT 2018 edu/illinois/cs/cs125/sortlib/
595 Sun Apr 15 16:00:08 CDT 2018 edu/illinois/cs/cs125/sortlib/Sorting.class
Creating And Publishing Your Own Libraries
It’s a big moment as a programmer to begin sharing your code with others.
-
Other people may use your code! That’s exciting.
-
They’ll complain when it doesn’t work. That’s frustrating.
-
They’ll want you to document it carefully. That’s worth doing.
Packaging Your Code
Java’s package
statement allows you to put your class into a package:
// Declare that this class is part of this package
package edu.illinois.cs.cs125.sortlib;
public class Sorting {
}
You can then import it in another project like this:
import edu.illinois.cs.cs125.sortlib.Sorting;
Java Package Repositories
Java has several existing code repositories where developers publish their work:
repositories {
// Look for dependencies on jcenter
jcenter()
// Look for dependencies on maven Central
mavenCentral()
}
jitpack.io
jitpack.io
is another example and possibly easier to use
if you are maintaining your code on GitHub.
IntelliJ Example Using jitpack.io
-
Link to starter app: https://github.com/cs125-illinois/lecture-packaging-app
-
Link to starter library: https://github.com/cs125-illinois/sortlib
build.gradle
with jitpack.io
apply plugin: 'java'
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
compile 'com.google.guava:guava:24.1-jre'
compile 'com.github.cs125-illinois:sortlib:0.0.1'
}
Questions?
I know that this is dull and a bit confusing. Post on the forum if you need help!
Final Project Discussion
We’ve released the MP7 final project specification. Here’s an overview:
-
You need to build an Android app. If your development environment isn’t working you’ll need to address that.
-
You need to design a simple UI. We’ll provide help with that in lab.
-
You’ll need to finish your UI mockup in a week.
-
You need to use a new web API, software library, or Android feature.
-
You need to commit your work to GitHub.
-
You need to work with someone in your lab section.
-
Other than that, what to do is up to you.
Final Project Evaluation
You’ll demo your final project in lab during the last week of class: either 5/1 or 5/2.
-
You will probably need to record a YouTube video or screencast of your app so that we can complete the app presentations in one lab section.
-
Grading will be generous.
Final Project Fair
On Thursday 5/3 (Reading Day), we’ll hold our first Final Project Fair from 4–6PM in Siebel.
-
The course staff will be on hand to review the final projects and select a few that we consider to be the most impressive.
-
If you had never programmed before this semester and build something simple and cool, that’s impressive.
-
Participation is optional…
-
…but worth 1% extra credit.
-
We’ll be handing out more extra credit to the best projects, and to the lab sections that produce the best final projects.
Announcements
-
MP7 (the final project) is out. Please get started!
-
The anonymous feedback form remains available on the course website. Use it to give us feedback!
-
My office hours continue today at 11AM in the lounge outside of Siebel 0226.