Skip to content

Latest commit

 

History

History
257 lines (174 loc) · 5.23 KB

java.md

File metadata and controls

257 lines (174 loc) · 5.23 KB

Java

NOT PORTED YET.

Books

Head First Design Patterns

SDKman - Install and Manage Multiple Versions of Java at the same time

See SDKman page.

Show Java Classpath

Since the java -cp / java -classpath is one huge string of colon separated paths, it's nicer to show them one per line using the scripts in DevOps-Bash-tools or DevOps-Perl-tools repos:

java_show_classpath.sh
java_show_classpath.pl

Crude shell pipeline to do similar:

ps -ef |
grep java |
tee /dev/stderr |
awk '{print $2}' |
xargs -L1 jinfo |
grep java.class.path  |
tr ':' '\n'

although if it's just jinfo you're missing in the $PATH it would be better to just:

PATH="$PATH:/path/to/bin/containing/jinfo" java_show_classpath.sh

Inspect JAR contents

Java jar files are just tars of the byte-compiled Java classes.

You can inspect them using the good old unix tar command, eg.:

jar tf mysql-connector-j-*.jar

or

tar tvf mysql-connector-j-*.jar

The directory layout of the class files corresponds to the class hierarchy eg. is accessed as com.mysql.jdbc.Driver in Java code.

Java Decompilers

Use these to decompile JAR or .class files to read the Java source code.

Using DevOps-Bash-tools repo:

For a GUI:

jd_gui.sh "$jar_or_class_file"

or

bytecode_viewer.sh

For command line output:

cfr.sh "$jar_or_class_file"

or

procyon.sh "$jar_or_class_file"

Libraries

Some libraries of interest:

  • Faker - generates fake but realistic data for unit testing

JShell

Command line or interactive Java Shell.

Java 9+:

$ jshell
|  Welcome to JShell -- Version 21.0.4
|  For an introduction type: /help intro

jshell>

See also Groovy which is one of my favourite languages and has a shell:

groovysh

JBang

https://www.jbang.dev/

Packages executable self-contained source-only Java programs.

Install using SDKman:

sdk install jbang

Create a source code CLI program:

jbang init -t cli hellocli.java

Reading the source code it shebangs jbang and annotates the class with some metadata for jbang.

The first run downloads the dependencies mentioned in the source code

$ ./hellocli.java --help
[jbang] Resolving dependencies...
[jbang]    info.picocli:picocli:4.6.3
[jbang] Dependencies resolved
[jbang] Building jar for hellocli.java...
Usage: hellocli [-hV] <greeting>
hellocli made with jbang
      <greeting>   The greeting to print
  -h, --help       Show this help message and exit.
  -V, --version    Print version information and exit.
$ ./hellocli.java JBANG!
Hello JBANG!

Automatic fetches any dependencies referenced in the source code using //DEPS group:artifact:version comments or @Grab annotations.

Even downloads a JDK if needed.

This makes portable Java scripting easier.

JBang CLI

jbang -c 'Java_code'

Example of JBang CLI using Faker library to output random names:

// DEPS com.github.javafaker.javafaker:1.0.2

import com.github.javafaker.Faker

Faker faker = new Faker()
cat > /tmp/faker.java <<EOF
//usr/bin/env jbang "$0" "$@" ; exit $?
//DEPS com.github.javafaker:javafaker:1.0.2

import com.github.javafaker.Faker;
import java.util.stream.Stream;

public class faker {
    public static void main(String[] args) {
      Faker faker = new Faker();
      Stream.generate(faker.name()::fullName)
          .filter(s -> s.contains("Hari"))
          .forEach(s -> System.out.println(s + " is Awesome"));
    }
}
EOF
jbang /tmp/faker.java

See also Groovy which is one of my favourite languages and wish I had more excuses to code it in other than:

Readme Card

GraalJS

:octocat: oracle/graaljs

JavaScript engine running on JVM via GraalVM.

ECMAScript-compliant runtime to execute JavaScript and Node.js applications on JVM with benefits of GraalVM stack including interoperability with Java.

Clojure

https://clojure.org/

Just a jar, no dependency like Scala predef.

java -jar my.jar

Ported from various private Knowledge Base pages 2010+