Sumary

  • Testing a single-file source program
  • A Maven project: jdbc and csv export

Today Oracle just released JDK 12. Like most developers, I am still using JDK 1.7 or 1.8. But I am curious about it. So I spent some time on JDK 11.

Single-file source code

  • Basically you do not need to compile first before you run.
  • Java single-source has no dependency management: you still have to set up classpath just like you run a compiled Java class file.
  • Groovy can do this long time ago and has a dependency management tool called Grape
  • Both Go and dotnet core have built-in dependency management

    // java ./Hello.java
    
    import picocli.CommandLine;
    import picocli.CommandLine.Option;
    import picocli.CommandLine.Parameters;
    
    import java.io.File;
    
    public class Hello implements Runnable 
    {
    @Option(names = { "-v", "--verbose" }, description = "Verbose mode. Helpful for troubleshooting. " +
    "Multiple -v options increase the verbosity.")
    private boolean[] verbose = new boolean[0];
    
    @Option(names = { "-h", "--help" }, usageHelp = true,
    description = "Displays this help message and quits.")
    private boolean helpRequested = false;
    
    @Parameters(arity = "1..*", paramLabel = "FILE", description = "File(s) to process.")
    private File[] inputFiles;
    
    public void run() {
        if (verbose.length > 0) {
            System.out.println(inputFiles.length + " files to process...");
        }
        if (verbose.length > 1) {
            for (File f : inputFiles) {
                System.out.println(f.getAbsolutePath());
            }
        }
    }
    
    public static void main(String[] args) {
        CommandLine.run(new Hello(), args);
    }
    }

It does work

Check my github repo for more examples

A sample Maven project

In Maven project, first step is to properly set it up the plugins for compiling and packaging.

In my Maven project, I tested accessing Postgres db and creating csv file from a query.