Giter Club home page Giter Club logo

iandarwin / javasrc Goto Github PK

View Code? Open in Web Editor NEW
239.0 34.0 150.0 50.19 MB

Ian's Collected code examples from the O'Reilly Java Cookbook & elsewhere

Home Page: http://javacook.darwinsys.com/

License: Other

HTML 10.11% Shell 0.27% Java 86.13% Python 0.05% PLpgSQL 0.01% C 2.32% Makefile 0.24% JavaScript 0.21% PostScript 0.05% Perl 0.12% Awk 0.04% C++ 0.13% XSLT 0.04% R 0.10% COBOL 0.03% C# 0.02% Fortran 0.01% CSS 0.09% Batchfile 0.03% Ruby 0.01%
java basics environment

javasrc's Introduction

JavaCook Source Files (javasrc)

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.

Note to self

Don’t delete files without checking if they’re still used in the Java Cookbook repo.

Origin

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

Technical Issues Compiling This Code

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 bisect (maybe start 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.

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

  • 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. 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 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 do 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 or del/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 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 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

Misc. ToDo

CONSIDER Moving all "compilation error" methods in $js into "dontcompile/"

javasrc's People

Contributors

cmawata avatar dependabot[bot] avatar iandarwin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

javasrc's Issues

org.junit and org.hamcrest do not exist

New to maven and git so hope this is correct way to notify you?

Running Maven on the command line (Maven binares for Windows in a Cygwin terminal); fails with the following errors (root of path cut to save space):

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project javasrc: Compilation failure: Compilation failure:
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[3,17] package org.junit does not exist
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[4,24] package org.junit does not exist
[ERROR] /src/main/java/testing/PersonTest.java:[3,1] package org.junit does not exist
[ERROR] /src/main/java/testing/PersonTest.java:[4,24] package org.junit does not exist
[ERROR] /src/main/java/testing/HamcrestDemo.java:[3,27] package org.hamcrest does not exist
[ERROR] /src/main/java/testing/HamcrestDemo.java:[3,1] static import only from classes and interfaces
[ERROR] /src/main/java/testing/HamcrestDemo.java:[4,27] package org.hamcrest does not exist
[ERROR] /src/main/java/testing/HamcrestDemo.java:[4,1] static import only from classes and interfaces
[ERROR] /src/main/java/testing/HamcrestDemo.java:[5,27] package org.hamcrest does not exist
[ERROR] /src/main/java/testing/HamcrestDemo.java:[5,1] static import only from classes and interfaces
[ERROR] /src/main/java/testing/HamcrestDemo.java:[6,24] package org.junit does not exist
[ERROR] /src/main/java/testing/HamcrestDemo.java:[6,1] static import only from classes and interfaces
[ERROR] /src/main/java/testing/HamcrestDemo.java:[8,17] package org.junit does not exist
[ERROR] /src/main/java/testing/IntegerTest.java:[3,23] package junit.framework does not exist
[ERROR] /src/main/java/testing/IntegerTest.java:[9,34] cannot find symbol
[ERROR] symbol: class TestCase
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[10,10] cannot find symbol
[ERROR] symbol: class Test
[ERROR] location: class lang.ArrayEqualsWorks
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[18,10] cannot find symbol
[ERROR] symbol: class Test
[ERROR] location: class lang.ArrayEqualsWorks
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[25,10] cannot find symbol
[ERROR] symbol: class Test
[ERROR] location: class lang.ArrayEqualsWorks
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[32,10] cannot find symbol
[ERROR] symbol: class Test
[ERROR] location: class lang.ArrayEqualsWorks
[ERROR] /src/main/java/testing/PersonTest.java:[10,10] cannot find symbol
[ERROR] symbol: class Test
[ERROR] location: class testing.PersonTest
[ERROR] /src/main/java/testing/HamcrestDemo.java:[13,10] cannot find symbol
[ERROR] symbol: class Test
[ERROR] location: class testing.HamcrestDemo
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[15,17] cannot find symbol
[ERROR] symbol: method assertFalse(java.lang.String,boolean)
[ERROR] location: class lang.ArrayEqualsWorks
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[22,17] cannot find symbol
[ERROR] symbol: method assertFalse(java.lang.String,boolean)
[ERROR] location: class lang.ArrayEqualsWorks
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[29,17] cannot find symbol
[ERROR] symbol: method assertFalse(java.lang.String,boolean)
[ERROR] location: class lang.ArrayEqualsWorks
[ERROR] /src/main/java/lang/ArrayEqualsWorks.java:[36,17] cannot find symbol
[ERROR] symbol: method assertFalse(java.lang.String,boolean)
[ERROR] location: class lang.ArrayEqualsWorks
[ERROR] /src/main/java/testing/PersonTest.java:[14,17] cannot find symbol
[ERROR] symbol: method assertEquals(java.lang.String,java.lang.String,java.lang.String)
[ERROR] location: class testing.PersonTest
[ERROR] /src/main/java/testing/HamcrestDemo.java:[17,31] cannot find symbol
[ERROR] symbol: method containsString(java.lang.String)
[ERROR] location: class testing.HamcrestDemo
[ERROR] /src/main/java/testing/HamcrestDemo.java:[18,31] cannot find symbol
[ERROR] symbol: method equalTo(java.lang.String)
[ERROR] location: class testing.HamcrestDemo
[ERROR] /src/main/java/testing/HamcrestDemo.java:[19,35] cannot find symbol
[ERROR] symbol: method containsString(java.lang.String)
[ERROR] location: class testing.HamcrestDemo
[ERROR] /src/main/java/testing/IntegerTest.java:[13,17] cannot find symbol
[ERROR] symbol: method assertEquals(int,int)
[ERROR] location: class testing.IntegerTest
[ERROR] /src/main/java/testing/IntegerTest.java:[15,17] cannot find symbol
[ERROR] symbol: method assertEquals(int,int)
[ERROR] location: class testing.IntegerTest
[ERROR] /src/main/java/testing/IntegerTest.java:[18,25] cannot find symbol
[ERROR] symbol: method fail(java.lang.String)
[ERROR] location: class testing.IntegerTest

Missing I18NUtil.java file.

Hi!
In the Recipe 3.11 you mention the I18NUtil.java file and state that it is available in the book's code distribution (Java Cookbook 4th, ed. pg. 86). However I couldn't find this file neither in this repository, nor in the "darwinsys-api" repo. Could you add this file, please.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.