Skip to content

Collected code examples from the O'Reilly Java Cookbook 3d Edn

License

Notifications You must be signed in to change notification settings

Girish-chandraiah/javasrc

 
 

Repository files navigation

JavaCook Source Files (javasrc)

This is my assorted collection of (you guessed it) Java source. It remains focused on building with Maven and Eclipse.

About a third of these files are featured in my O’Reilly Java Cookbook. I use a script (copyCodeSamples) to pre-format these for inclusion in the O’Reilly publishing toolchain; these will have // tag::foo[] and // end::foo[] comments to mark sections for mechanical inclusion into the book. These are just comments to Java tooling and can be completely ignored by people looking at the source code normally. 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.

Structured as Maven Modules

This repo consists of about a dozen Maven modules (aka subdirectories :-)), 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

  • restdemo - REST

  • spark - Apache SPARK

  • unsafe - com.sun.Unsafe

  • testing - code that expands on testing (other modules have normal unit tests)

  • xml - XML parsing and formatting with various APIs

Prior to the Java Cookbook revision (late 2019/early 2020), 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. Right now there is a lot of stuff in the "main" module, but this will get broken out into a few additional modules.

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 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.

I broke it (but it’s easy to fix)

Unfortunately for those who already had the original all-in-one javasrc checked out and an Eclipse project created in that directory, you should do the following:

  • In Eclipse, delete javasrc project (do NOT check 'delete contents on disk'!);

  • Delete the old target folder (only): {rm -r or del/s} javasrc/target;

  • Do a "git pull" to rearrange the project and get the extra pom files;

  • Back in Eclipse, File->New Java Project->browse to (but not into) workspace/javasrc/main. Set project name to javasrc-main. If asked to upgrade the JDK release, say OK. If asked to create a module-info, click Do Not Create. Click OK/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.

The older 'javasrcee' repository was originally formed by splitting it off from this repository, several years ago. Now, with this modularization, it has been merged back in, as the ee module.

No module-info

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.

Notes on Individual Modules

testing

Works under "mvn test". Compiles as an Eclipse project, but can’t run due to a Junit 5 loading conflict (pull requests welcome on this one, thanks).

Building

  • You MUST HAVE Java 11+ to successfully compile this whole package. Sorry if you are on some relic platform that doesn’t have Java 9 or later. Even OpenBSD has Java 17 (Thanks Kurt!). Also sorry if your organization is stuck on Java 8 due to application server issues.

  • I am using Eclipse for most of my development, and Maven for building, and Jenkins for automated building. Most Ant scripts have been removed; the few that remain will eventually be converted to Maven exec:java configurations.

  • Building with Eclipse tested with Eclipse 4.x; MUST HAVE "m2e" (free in the Eclipse Marketplace) Compiles and tests pass.

  • Building with Maven 3.x works and tests pass.

  • Building with other platforms? Good luck, but let me know if it works.

  • If you really need Java 8, try removing the module-info files. But you’re on your own. And you’ll need some exclusions for files that use newer language features.

Ian Darwin

Java Cookbook author

About

Collected code examples from the O'Reilly Java Cookbook 3d Edn

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 86.3%
  • HTML 10.3%
  • C 2.3%
  • Shell 0.2%
  • Makefile 0.2%
  • JavaScript 0.2%
  • Other 0.5%