← Back to winterbe/java8-tutorial

How to Deploy & Use winterbe/java8-tutorial

Modern Java - A Guide to Java 8

Prerequisites

  • Java Development Kit (JDK) 8 or later - This tutorial requires Java 8 or newer to run the examples
  • Java IDE (optional but recommended) - Any Java IDE like IntelliJ IDEA, Eclipse, or NetBeans
  • Maven (optional) - For building and managing dependencies if you want to use the project structure

Installation

  1. Clone the repository:

    git clone https://github.com/winterbe/java8-tutorial.git
    cd java8-tutorial
    
  2. Verify Java installation:

    java -version
    javac -version
    
  3. Set up your IDE (if using one):

    • Import the project as a Maven project
    • Configure the JDK to use Java 8 or later

Configuration

This is a tutorial project with no external dependencies or configuration required. The examples are self-contained and use only standard Java libraries.

If you want to run specific examples:

  • Nashorn JavaScript engine: Available in JDK 8-14 (deprecated in JDK 11, removed in JDK 15+)
  • File operations: Ensure you have read/write permissions in the project directory
  • Path references: Some examples reference files in the res/ directory (e.g., res/nashorn1.js)

Build & Run

Running Individual Examples

Most examples are structured as standalone Java classes with a main method. To run them:

  1. Using an IDE:

    • Open the desired Java file (e.g., Streams5.java, Nashorn11.java)
    • Run the main method directly
  2. Using command line:

    # Compile the specific file
    javac -cp . src/com/winterbe/java8/samples/stream/Streams5.java
    
    # Run the compiled class
    java -cp . com.winterbe.java8.samples.stream.Streams5
    

Running Multiple Examples

The tutorial is designed to be run example by example. Each class contains multiple test methods (commented out by default). To run a specific test:

  1. Uncomment the desired test method in the main method
  2. Comment out the others
  3. Run the class

Example from Streams5.java:

public static void main(String[] args) {
    List<String> strings = Arrays.asList("d2", "a2", "b1", "b3", "c");
    test8(strings);  // Uncomment this line to run test8
}

Deployment

This is a tutorial/learning project, not a deployable application. However, if you want to share or host the examples:

Options:

  1. GitHub Pages - Create a documentation site with runnable examples
  2. Jupyter Notebooks - Convert examples to interactive notebooks
  3. Educational Platform - Deploy as part of a Java learning course

For Production Java Applications:

If you're building on the concepts learned here:

  1. Build with Maven:

    mvn clean compile package
    
  2. Deploy to:

    • Cloud Platforms: Heroku, AWS Elastic Beanstalk, Google App Engine
    • Container Services: Docker + Kubernetes, AWS ECS, Google Cloud Run
    • Traditional Servers: Tomcat, Jetty, WildFly

Troubleshooting

Common Issues and Solutions

1. Java Version Compatibility

  • Issue: Code doesn't compile with Java 8
  • Solution: Ensure you're using JDK 8 or later. Some features may require newer versions.

2. Nashorn Engine Not Found

  • Issue: javax.script or Nashorn classes not found
  • Solution: Nashorn was deprecated in JDK 11 and removed in JDK 15+. Use JDK 8-10 for Nashorn examples.

3. File Path Issues

  • Issue: File not found exceptions when running file examples
  • Solution: Ensure the res/ directory exists and contains the required files. Use absolute paths if needed.

4. Compilation Errors

  • Issue: Syntax errors or missing imports
  • Solution: Check that you're using the correct Java version and that all necessary imports are present.

5. Stream Operations Not Working

  • Issue: Stream operations throwing exceptions
  • Solution: Ensure streams are not being operated on after being closed. Streams can only be consumed once.

6. Lambda Expression Issues

  • Issue: Lambda syntax errors
  • Solution: Verify that the functional interface has exactly one abstract method. Use @FunctionalInterface annotation for clarity.

7. IDE Configuration

  • Issue: IDE not recognizing packages or classes
  • Solution: Configure the project SDK to use Java 8+, set up proper source folders, and ensure the project is imported correctly.

Debug Tips

  • Use System.out.println() statements to trace execution flow
  • Enable Java assertions with -ea flag for debugging
  • Use your IDE's debugger to step through lambda expressions and stream operations
  • Check the Java API documentation for the latest method signatures and behaviors