This is my assorted collection of (you guessed it) Java source. It remains focused on building with bleeding-edge Java (currently 22), Maven, and the IntelliJ IDEA IDE. Some modules may still build with the Eclipse IDE; for those that don’t, pull requests welcome.
As of 2024-04-14, this does not work on Eclipse,
even with the "Java 22 updater"
(https://marketplace.eclipse.org/content/java-22-support-eclipse-2024-03-431).
Their compiler
fails to realize that "classless main" doesn’t need (and isn’t allowed to have)
a package statement.
This seems to be an Eclipse bug, and fails even with the source attribute set to
22 and using a Java 22 JDK with "Preview" checked.
Further, their library appears to be missing at least two methods in the FFI Arena
class,
causing CallCFromJava
to fail to compile.
Don’t delete files without checking if they’re still used in the Java Cookbook repo.
These files began as my personal collection of Java examples, back when Java was first unleashed on a public (well, developers) accustomed to having to choose between portability and performance. I turned what I’d learned from working with Java into a course for Learning Tree and then the first edition of the Java Cookbook. Now in its fourth or fifth edition, this book consists of several hundred how-to "recipes" each discussing a particular problem that devs have to code, how to solve it, and illustrated with one or more code examples.
The Contents of the Fourth Edition looks like this:
-
1 Getting Started: Compiling and Running Java
-
2 Interacting with the Environment
-
3 String and Things
-
4 Pattern Matching with Regular Expressions
-
5 Numbers
-
6 Dates and Times
-
7 Structuring Data with Java
-
8 Object-oriented Techniques
-
9 Functional Programming
-
10 Input and Output
-
11 Data Science and R
-
12 Networking: Clients
-
13 Networking: Server-Side Java
-
14 Processing JSON Data
-
15 Packages and Packaging
-
16 Threaded Java
-
17 Reflection, or "A Class Named Class"
-
18 Using Java with Other Languages
-
Afterword
-
Appendix: Java Then and Now
If you want to compile everything, the minimum Java release is currently 22.
You can compile for Java 17 by checking out the java-17 branch, but it’s not actively maintained.
Or you could just add exclusions for the few files that use newer features.
For releases before that, I don’t have branches or tags, but if somebody wants to 'git bisect'
(maybe starting around commit 628efafe
) and tell me what works for 11,
I shall be happy to add a tag.
Some files use Java "Preview" features, which work if you’re compiling with the Maven POM file provided.
If not, you’ll probably need the argument --enable-preview
(and maybe --source-22
) to get these to compile.
As mentioned above, a goodly number of these files are featured in my O’Reilly
Java Cookbook.
To avoid copy-and-paste errors (or, worse, retyping!),
I use a script (copyCodeSamples
) to pre-format these for inclusion in the O’Reilly publishing toolchain;
these source code files will have // tag::foo[]
and // end::foo[]
comments to mark sections for
mechanical inclusion into the book, and // <n>
comments to refer to the code in the text.
These are all just comments as far as Java tooling is concerned,
and can be completely ignored by people looking at the source code normally.
N.B. Not all files with these comments necessarily appear in the book;
files are included from the manuscript using
asciidoctor's include
mechanism and,
when a code sample gets dropped from the book, I have no motivation
to edit the tags out of the code, in case I use it elsewhere.
This repo consists of about a dozen Maven modules (aka subdirectories :-)). The reason for this is discussed below (TL;DR: Dependencies). The layout is as follows:
-
main - everything that isn’t in one of the others
-
desktop - GUI/Graphics, JavaFX, comm devices, etc.
-
ee - Jakarta EE (Java EE, J2EE) stuff
-
graal - Only works with graal vm
-
jlink - Not ready yet?
-
json - JSON parsing and formatting with various APIs
-
lombokdemo - Project Lombok - not working ATM
-
Rdemo-web - NOT a maven module - some R demos
-
restdemo - REST
-
spark - Apache SPARK
-
unsafe - com.sun.Unsafe
-
testing - code that shows more details on testing (other modules have normal unit tests)
-
testing-spock - testing with the Spock framework
-
unsafe - you shouldn’t want to know
-
xml - XML parsing and formatting with various APIs
-
sidebyside - NOT a maven module - some non-Java versions of demos
A few modules are disabled because I don’t have time to fix them now.
Prior to the revision (late 2019/early 2020) for the Fourth Edition of Java Cookbook, this repo was one single module with no sub-modules. This became difficult to manage and to use, since, to compile anything, a user would have to wait for Maven to download all the dependencies and their dependencies and theirs… That revision seemed like a good time to split the whole shebang into smaller pieces. Each module has its own POM file, Maven structure, etc.
A main reason was that the CLASSPATH was becoming unmanageable.
Not to mention MODULEPATH, and the time it takes to build the whole thing.
Now that it’s done this way, you can choose to just build one module
or another, without having Maven download 3/4 of the Internet for
dependencies. Just cd
into one of the module directories and
invoke mvn compile
there. Or you can do them all by invoking mvn
in
the top-level directory.
Useful mvn targets include compile
and test
.
Do not use mvn package
as the jar files won’t be useful on their own.
Unfortunately for those who already had the old all-in-one javasrc project from before 2020
checked out, and an Eclipse project created in that directory,
If you do not have any changes worth keeping, then just delete
the entire project and start over.
If you perform the git clone
inside Eclipse, there’s a "Create projects from import" checkbox
that will make all the projects for you. If not, clone the repot and go to the step "Back in Eclipse" below.
If you do have changes you want to keep, then do the following:
-
In Eclipse, delete the
javasrc
project (do NOT check 'delete contents on disk'!); -
Delete the old target folder (only): {
rm -r
ordel/s
} javasrc/target; -
Do a "git pull" to rearrange the project and get the extra pom files;
-
Deal with any files that didn’t get moved,e.g., because of conflicts;
-
Back in Eclipse,
File→New Java Project→browse to (but not into) workspace/javasrc/main
. Set project name tojavasrc-main
. If asked to upgrade the JDK release, say OK. If asked to create a module-info, click Do Not Create. ClickOK/Finish
. -
You may want to create some or all of the other projects such as xml, jlink, spark, unsafe, … Do this same way as previous step: File→new Java project etc. Recommmended names are javasrc-xml, javasrc-unsafe, etc.
It’s easier if you use IntelliJ: just open the module you want via Open Project. But IntelliJ flubs out if you have even one file in a project that won’t compile.
The older 'javasrcee' repository was originally formed by splitting
it off from this repository, years ago. Now, with this modularization,
it has been merged back in, as the ee
module.
There are no module-info.java files in most of the subdirectories because this is not meant to be built and used as a library or even as a cohesive software base. A few that need them for imports &c have them.
- testing
-
Works under "mvn test". Compiles as an Eclipse project, but can’t run there due to a Junit 5 loading conflict (pull requests welcome on this one, thanks).
-
You MUST HAVE a current release of Java (see notes at beginning) to successfully compile this whole package. Sorry if you are on some relic platform that doesn’t have current Java. Even OpenBSD has Java 21 (Thanks Kurt!). Also sorry if your organization is stuck on an ancient Java due to application server issues.
-
I am using Eclipse for most of my development, and Maven for building, and Jenkins for automated building. The Ant scripts have been removed (except for a few in the ee module under ejb2 and rmi; the few that remain will someday get deleted, or converted to Maven exec:java configurations).
-
Building with Maven 3.x works and tests pass.
-
Building with Eclipse tested with Eclipse 4.x; MUST HAVE "m2e" (free in the Eclipse Marketplace) Compiles and tests pass.
-
Building with other platforms? Good luck, but let me know if it works.
Ian Darwin
Java Cookbook author